blob: a0c90572d0e57900e969f94aba4767305a16ec96 [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 Alpef2b3b2d42015-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 Alpef2b3b2d42015-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
90static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
91{
92 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
93
94 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
95 return -EMSGSIZE;
96
97 skb_put(skb, TLV_SPACE(len));
98 tlv->tlv_type = htons(type);
99 tlv->tlv_len = htons(TLV_LENGTH(len));
100 if (len && data)
101 memcpy(TLV_DATA(tlv), data, len);
102
103 return 0;
104}
105
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100106static void tipc_tlv_init(struct sk_buff *skb, u16 type)
107{
108 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
109
110 TLV_SET_LEN(tlv, 0);
111 TLV_SET_TYPE(tlv, type);
112 skb_put(skb, sizeof(struct tlv_desc));
113}
114
115static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
116{
117 int n;
118 u16 len;
119 u32 rem;
120 char *buf;
121 struct tlv_desc *tlv;
122 va_list args;
123
124 rem = tipc_skb_tailroom(skb);
125
126 tlv = (struct tlv_desc *)skb->data;
127 len = TLV_GET_LEN(tlv);
128 buf = TLV_DATA(tlv) + len;
129
130 va_start(args, fmt);
131 n = vscnprintf(buf, rem, fmt, args);
132 va_end(args);
133
134 TLV_SET_LEN(tlv, n + len);
135 skb_put(skb, n);
136
137 return n;
138}
139
Richard Alped0796d12015-02-09 09:50:04 +0100140static struct sk_buff *tipc_tlv_alloc(int size)
141{
142 int hdr_len;
143 struct sk_buff *buf;
144
145 size = TLV_SPACE(size);
146 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
147
148 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
149 if (!buf)
150 return NULL;
151
152 skb_reserve(buf, hdr_len);
153
154 return buf;
155}
156
157static struct sk_buff *tipc_get_err_tlv(char *str)
158{
159 int str_len = strlen(str) + 1;
160 struct sk_buff *buf;
161
162 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
163 if (buf)
164 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
165
166 return buf;
167}
168
169static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
170 struct tipc_nl_compat_msg *msg,
171 struct sk_buff *arg)
172{
173 int len = 0;
174 int err;
175 struct sk_buff *buf;
176 struct nlmsghdr *nlmsg;
177 struct netlink_callback cb;
178
179 memset(&cb, 0, sizeof(cb));
180 cb.nlh = (struct nlmsghdr *)arg->data;
181 cb.skb = arg;
182
183 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
184 if (!buf)
185 return -ENOMEM;
186
187 buf->sk = msg->dst_sk;
188
189 do {
190 int rem;
191
192 len = (*cmd->dumpit)(buf, &cb);
193
194 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
195 struct nlattr **attrs;
196
197 err = tipc_nlmsg_parse(nlmsg, &attrs);
198 if (err)
199 goto err_out;
200
201 err = (*cmd->format)(msg, attrs);
202 if (err)
203 goto err_out;
204
205 if (tipc_skb_tailroom(msg->rep) <= 1) {
206 err = -EMSGSIZE;
207 goto err_out;
208 }
209 }
210
211 skb_reset_tail_pointer(buf);
212 buf->len = 0;
213
214 } while (len);
215
216 err = 0;
217
218err_out:
219 kfree_skb(buf);
220
221 if (err == -EMSGSIZE) {
222 /* The legacy API only considered messages filling
223 * "ULTRA_STRING_MAX_LEN" to be truncated.
224 */
225 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
226 char *tail = skb_tail_pointer(msg->rep);
227
228 if (*tail != '\0')
229 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
230 REPLY_TRUNCATED);
231 }
232
233 return 0;
234 }
235
236 return err;
237}
238
239static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
240 struct tipc_nl_compat_msg *msg)
241{
242 int err;
243 struct sk_buff *arg;
244
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100245 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
246 return -EINVAL;
247
Richard Alped0796d12015-02-09 09:50:04 +0100248 msg->rep = tipc_tlv_alloc(msg->rep_size);
249 if (!msg->rep)
250 return -ENOMEM;
251
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100252 if (msg->rep_type)
253 tipc_tlv_init(msg->rep, msg->rep_type);
254
Richard Alpe44a8ae92015-02-09 09:50:10 +0100255 if (cmd->header)
256 (*cmd->header)(msg);
257
Richard Alped0796d12015-02-09 09:50:04 +0100258 arg = nlmsg_new(0, GFP_KERNEL);
259 if (!arg) {
260 kfree_skb(msg->rep);
261 return -ENOMEM;
262 }
263
264 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
265 if (err)
266 kfree_skb(msg->rep);
267
268 kfree_skb(arg);
269
270 return err;
271}
272
Richard Alpe9ab15462015-02-09 09:50:05 +0100273static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
274 struct tipc_nl_compat_msg *msg)
275{
276 int err;
277 struct sk_buff *doit_buf;
278 struct sk_buff *trans_buf;
279 struct nlattr **attrbuf;
280 struct genl_info info;
281
282 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
283 if (!trans_buf)
284 return -ENOMEM;
285
Richard Alpec3d6fb82015-05-06 13:58:54 +0200286 err = (*cmd->transcode)(cmd, trans_buf, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100287 if (err)
288 goto trans_out;
289
290 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
291 sizeof(struct nlattr *), GFP_KERNEL);
292 if (!attrbuf) {
293 err = -ENOMEM;
294 goto trans_out;
295 }
296
297 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
298 (const struct nlattr *)trans_buf->data,
299 trans_buf->len, NULL);
300 if (err)
301 goto parse_out;
302
303 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
304 if (!doit_buf) {
305 err = -ENOMEM;
306 goto parse_out;
307 }
308
309 doit_buf->sk = msg->dst_sk;
310
311 memset(&info, 0, sizeof(info));
312 info.attrs = attrbuf;
313
314 err = (*cmd->doit)(doit_buf, &info);
315
316 kfree_skb(doit_buf);
317parse_out:
318 kfree(attrbuf);
319trans_out:
320 kfree_skb(trans_buf);
321
322 return err;
323}
324
325static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
326 struct tipc_nl_compat_msg *msg)
327{
328 int err;
329
330 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
331 return -EINVAL;
332
333 err = __tipc_nl_compat_doit(cmd, msg);
334 if (err)
335 return err;
336
337 /* The legacy API considered an empty message a success message */
338 msg->rep = tipc_tlv_alloc(0);
339 if (!msg->rep)
340 return -ENOMEM;
341
342 return 0;
343}
344
Richard Alped0796d12015-02-09 09:50:04 +0100345static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
346 struct nlattr **attrs)
347{
348 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
349
350 nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
351 NULL);
352
353 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
354 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
355 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
356}
357
Richard Alpec3d6fb82015-05-06 13:58:54 +0200358static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd,
359 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100360 struct tipc_nl_compat_msg *msg)
361{
362 struct nlattr *prop;
363 struct nlattr *bearer;
364 struct tipc_bearer_config *b;
365
366 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
367
368 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
369 if (!bearer)
370 return -EMSGSIZE;
371
372 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
373 return -EMSGSIZE;
374
375 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
376 return -EMSGSIZE;
377
378 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
379 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
380 if (!prop)
381 return -EMSGSIZE;
382 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
383 return -EMSGSIZE;
384 nla_nest_end(skb, prop);
385 }
386 nla_nest_end(skb, bearer);
387
388 return 0;
389}
390
Richard Alpec3d6fb82015-05-06 13:58:54 +0200391static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd,
392 struct sk_buff *skb,
Richard Alpe9ab15462015-02-09 09:50:05 +0100393 struct tipc_nl_compat_msg *msg)
394{
395 char *name;
396 struct nlattr *bearer;
397
398 name = (char *)TLV_DATA(msg->req);
399
400 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
401 if (!bearer)
402 return -EMSGSIZE;
403
404 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
405 return -EMSGSIZE;
406
407 nla_nest_end(skb, bearer);
408
409 return 0;
410}
411
Richard Alpef2b3b2d42015-02-09 09:50:06 +0100412static inline u32 perc(u32 count, u32 total)
413{
414 return (count * 100 + (total / 2)) / total;
415}
416
417static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
418 struct nlattr *prop[], struct nlattr *stats[])
419{
420 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
421 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
422
423 tipc_tlv_sprintf(msg->rep,
424 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
425 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
426 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
427 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
428 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
429 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
430
431 tipc_tlv_sprintf(msg->rep,
432 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
433 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
434 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
435 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
436 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
437 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
438
439 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
440 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
441 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
442 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
443
444 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
445 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
446 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
447 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
448
449 tipc_tlv_sprintf(msg->rep,
450 " Congestion link:%u Send queue max:%u avg:%u",
451 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
452 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
453 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
454}
455
456static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
457 struct nlattr **attrs)
458{
459 char *name;
460 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
461 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
462 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
463
464 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
465
466 nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
467 NULL);
468
469 nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
470 NULL);
471
472 name = (char *)TLV_DATA(msg->req);
473 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
474 return 0;
475
476 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
477 nla_data(link[TIPC_NLA_LINK_NAME]));
478
479 if (link[TIPC_NLA_LINK_BROADCAST]) {
480 __fill_bc_link_stat(msg, prop, stats);
481 return 0;
482 }
483
484 if (link[TIPC_NLA_LINK_ACTIVE])
485 tipc_tlv_sprintf(msg->rep, " ACTIVE");
486 else if (link[TIPC_NLA_LINK_UP])
487 tipc_tlv_sprintf(msg->rep, " STANDBY");
488 else
489 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
490
491 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
492 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
493 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
494
495 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
496 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
497 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
498
499 tipc_tlv_sprintf(msg->rep,
500 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
501 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
502 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
503 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
504 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
505 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
506 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
507
508 tipc_tlv_sprintf(msg->rep,
509 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
510 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
511 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
512 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
513 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
514 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
515 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
516
517 tipc_tlv_sprintf(msg->rep,
518 " TX profile sample:%u packets average:%u octets\n",
519 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
520 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
521 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
522
523 tipc_tlv_sprintf(msg->rep,
524 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
525 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
526 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
527 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
528 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
529 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
530 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
531 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
532 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
533
534 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
535 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
536 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
537 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
538 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
539 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
540 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
541
542 tipc_tlv_sprintf(msg->rep,
543 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
544 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
545 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
546 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
547 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
548 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
549
550 tipc_tlv_sprintf(msg->rep,
551 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
552 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
553 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
554 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
555 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
556 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
557
558 tipc_tlv_sprintf(msg->rep,
559 " Congestion link:%u Send queue max:%u avg:%u",
560 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
561 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
562 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
563
564 return 0;
565}
566
Richard Alpe357ebdb2015-02-09 09:50:07 +0100567static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
568 struct nlattr **attrs)
569{
570 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
571 struct tipc_link_info link_info;
572
573 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
574
575 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
576 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
Richard Alpe8b2e3452016-07-14 15:02:07 +0100577 nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME],
Kangjie Lu5fb71612016-07-14 15:02:06 +0100578 TIPC_MAX_LINK_NAME);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100579
580 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
581 &link_info, sizeof(link_info));
582}
583
Richard Alpec3d6fb82015-05-06 13:58:54 +0200584static int __tipc_add_link_prop(struct sk_buff *skb,
585 struct tipc_nl_compat_msg *msg,
586 struct tipc_link_config *lc)
Richard Alpe37e2d482015-02-09 09:50:08 +0100587{
Richard Alpec3d6fb82015-05-06 13:58:54 +0200588 switch (msg->cmd) {
589 case TIPC_CMD_SET_LINK_PRI:
590 return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value));
591 case TIPC_CMD_SET_LINK_TOL:
592 return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value));
593 case TIPC_CMD_SET_LINK_WINDOW:
594 return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value));
595 }
596
597 return -EINVAL;
598}
599
600static int tipc_nl_compat_media_set(struct sk_buff *skb,
601 struct tipc_nl_compat_msg *msg)
602{
Richard Alpe37e2d482015-02-09 09:50:08 +0100603 struct nlattr *prop;
Richard Alpec3d6fb82015-05-06 13:58:54 +0200604 struct nlattr *media;
605 struct tipc_link_config *lc;
606
607 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
608
609 media = nla_nest_start(skb, TIPC_NLA_MEDIA);
610 if (!media)
611 return -EMSGSIZE;
612
613 if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name))
614 return -EMSGSIZE;
615
616 prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP);
617 if (!prop)
618 return -EMSGSIZE;
619
620 __tipc_add_link_prop(skb, msg, lc);
621 nla_nest_end(skb, prop);
622 nla_nest_end(skb, media);
623
624 return 0;
625}
626
627static int tipc_nl_compat_bearer_set(struct sk_buff *skb,
628 struct tipc_nl_compat_msg *msg)
629{
630 struct nlattr *prop;
631 struct nlattr *bearer;
632 struct tipc_link_config *lc;
633
634 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
635
636 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
637 if (!bearer)
638 return -EMSGSIZE;
639
640 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name))
641 return -EMSGSIZE;
642
643 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
644 if (!prop)
645 return -EMSGSIZE;
646
647 __tipc_add_link_prop(skb, msg, lc);
648 nla_nest_end(skb, prop);
649 nla_nest_end(skb, bearer);
650
651 return 0;
652}
653
654static int __tipc_nl_compat_link_set(struct sk_buff *skb,
655 struct tipc_nl_compat_msg *msg)
656{
657 struct nlattr *prop;
658 struct nlattr *link;
Richard Alpe37e2d482015-02-09 09:50:08 +0100659 struct tipc_link_config *lc;
660
661 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
662
663 link = nla_nest_start(skb, TIPC_NLA_LINK);
664 if (!link)
665 return -EMSGSIZE;
666
667 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
668 return -EMSGSIZE;
669
670 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
671 if (!prop)
672 return -EMSGSIZE;
673
Richard Alpec3d6fb82015-05-06 13:58:54 +0200674 __tipc_add_link_prop(skb, msg, lc);
Richard Alpe37e2d482015-02-09 09:50:08 +0100675 nla_nest_end(skb, prop);
676 nla_nest_end(skb, link);
677
678 return 0;
679}
680
Richard Alpec3d6fb82015-05-06 13:58:54 +0200681static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd,
682 struct sk_buff *skb,
683 struct tipc_nl_compat_msg *msg)
684{
685 struct tipc_link_config *lc;
686 struct tipc_bearer *bearer;
687 struct tipc_media *media;
688
689 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
690
691 media = tipc_media_find(lc->name);
692 if (media) {
693 cmd->doit = &tipc_nl_media_set;
694 return tipc_nl_compat_media_set(skb, msg);
695 }
696
697 bearer = tipc_bearer_find(msg->net, lc->name);
698 if (bearer) {
699 cmd->doit = &tipc_nl_bearer_set;
700 return tipc_nl_compat_bearer_set(skb, msg);
701 }
702
703 return __tipc_nl_compat_link_set(skb, msg);
704}
705
706static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd,
707 struct sk_buff *skb,
Richard Alpe18178772015-02-09 09:50:09 +0100708 struct tipc_nl_compat_msg *msg)
709{
710 char *name;
711 struct nlattr *link;
712
713 name = (char *)TLV_DATA(msg->req);
714
715 link = nla_nest_start(skb, TIPC_NLA_LINK);
716 if (!link)
717 return -EMSGSIZE;
718
719 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
720 return -EMSGSIZE;
721
722 nla_nest_end(skb, link);
723
724 return 0;
725}
726
Richard Alpe44a8ae92015-02-09 09:50:10 +0100727static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
728{
729 int i;
730 u32 depth;
731 struct tipc_name_table_query *ntq;
732 static const char * const header[] = {
733 "Type ",
734 "Lower Upper ",
735 "Port Identity ",
736 "Publication Scope"
737 };
738
739 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
740
741 depth = ntohl(ntq->depth);
742
743 if (depth > 4)
744 depth = 4;
745 for (i = 0; i < depth; i++)
746 tipc_tlv_sprintf(msg->rep, header[i]);
747 tipc_tlv_sprintf(msg->rep, "\n");
748
749 return 0;
750}
751
752static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
753 struct nlattr **attrs)
754{
755 char port_str[27];
756 struct tipc_name_table_query *ntq;
757 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
758 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
759 u32 node, depth, type, lowbound, upbound;
760 static const char * const scope_str[] = {"", " zone", " cluster",
761 " node"};
762
763 nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
764 attrs[TIPC_NLA_NAME_TABLE], NULL);
765
766 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, nt[TIPC_NLA_NAME_TABLE_PUBL],
767 NULL);
768
769 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
770
771 depth = ntohl(ntq->depth);
772 type = ntohl(ntq->type);
773 lowbound = ntohl(ntq->lowbound);
774 upbound = ntohl(ntq->upbound);
775
776 if (!(depth & TIPC_NTQ_ALLTYPES) &&
777 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
778 return 0;
779 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
780 return 0;
781 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
782 return 0;
783
784 tipc_tlv_sprintf(msg->rep, "%-10u ",
785 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
786
787 if (depth == 1)
788 goto out;
789
790 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
791 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
792 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
793
794 if (depth == 2)
795 goto out;
796
797 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
798 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
799 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
800 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
801
802 if (depth == 3)
803 goto out;
804
805 tipc_tlv_sprintf(msg->rep, "%-10u %s",
Richard Alpe6a58f3e2016-05-17 16:57:37 +0200806 nla_get_u32(publ[TIPC_NLA_PUBL_KEY]),
Richard Alpe44a8ae92015-02-09 09:50:10 +0100807 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
808out:
809 tipc_tlv_sprintf(msg->rep, "\n");
810
811 return 0;
812}
813
Richard Alpe487d2a32015-02-09 09:50:11 +0100814static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
815 struct nlattr **attrs)
816{
817 u32 type, lower, upper;
818 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
819
820 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL], NULL);
821
822 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
823 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
824 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
825
826 if (lower == upper)
827 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
828 else
829 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
830
831 return 0;
832}
833
834static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
835{
836 int err;
837 void *hdr;
838 struct nlattr *nest;
839 struct sk_buff *args;
840 struct tipc_nl_compat_cmd_dump dump;
841
842 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
843 if (!args)
844 return -ENOMEM;
845
846 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
847 TIPC_NL_PUBL_GET);
848
849 nest = nla_nest_start(args, TIPC_NLA_SOCK);
850 if (!nest) {
851 kfree_skb(args);
852 return -EMSGSIZE;
853 }
854
855 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
856 kfree_skb(args);
857 return -EMSGSIZE;
858 }
859
860 nla_nest_end(args, nest);
861 genlmsg_end(args, hdr);
862
863 dump.dumpit = tipc_nl_publ_dump;
864 dump.format = __tipc_nl_compat_publ_dump;
865
866 err = __tipc_nl_compat_dumpit(&dump, msg, args);
867
868 kfree_skb(args);
869
870 return err;
871}
872
873static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
874 struct nlattr **attrs)
875{
876 int err;
877 u32 sock_ref;
878 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
879
880 nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK], NULL);
881
882 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
883 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
884
885 if (sock[TIPC_NLA_SOCK_CON]) {
886 u32 node;
887 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
888
889 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
890 NULL);
891
892 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
893 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
894 tipc_zone(node),
895 tipc_cluster(node),
896 tipc_node(node),
897 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
898
899 if (con[TIPC_NLA_CON_FLAG])
900 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
901 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
902 nla_get_u32(con[TIPC_NLA_CON_INST]));
903 else
904 tipc_tlv_sprintf(msg->rep, "\n");
905 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
906 tipc_tlv_sprintf(msg->rep, " bound to");
907
908 err = tipc_nl_compat_publ_dump(msg, sock_ref);
909 if (err)
910 return err;
911 }
912 tipc_tlv_sprintf(msg->rep, "\n");
913
914 return 0;
915}
916
Richard Alpe5bfc3352015-02-09 09:50:12 +0100917static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
918 struct nlattr **attrs)
919{
920 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
921
922 nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
923 NULL);
924
925 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
926 nla_data(media[TIPC_NLA_MEDIA_NAME]),
927 nla_len(media[TIPC_NLA_MEDIA_NAME]));
928}
929
Richard Alpe4b28cb52015-02-09 09:50:13 +0100930static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
931 struct nlattr **attrs)
932{
933 struct tipc_node_info node_info;
934 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
935
936 nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE], NULL);
937
938 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
939 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
940
941 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
942 sizeof(node_info));
943}
944
Richard Alpec3d6fb82015-05-06 13:58:54 +0200945static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd,
946 struct sk_buff *skb,
Richard Alped7cc75d2015-02-09 09:50:14 +0100947 struct tipc_nl_compat_msg *msg)
948{
949 u32 val;
950 struct nlattr *net;
951
952 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
953
954 net = nla_nest_start(skb, TIPC_NLA_NET);
955 if (!net)
956 return -EMSGSIZE;
957
Richard Alpe964f9502015-02-09 09:50:15 +0100958 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
959 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
960 return -EMSGSIZE;
961 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
962 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
963 return -EMSGSIZE;
964 }
Richard Alped7cc75d2015-02-09 09:50:14 +0100965 nla_nest_end(skb, net);
966
967 return 0;
968}
969
Richard Alpe3c261812015-02-09 09:50:16 +0100970static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
971 struct nlattr **attrs)
972{
973 __be32 id;
974 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
975
976 nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET], NULL);
977 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
978
979 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
980}
981
Richard Alpe5a81a632015-02-09 09:50:17 +0100982static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
983{
984 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
985 if (!msg->rep)
986 return -ENOMEM;
987
988 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
989 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
990
991 return 0;
992}
993
Richard Alped0796d12015-02-09 09:50:04 +0100994static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
995{
996 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +0100997 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +0100998
999 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +01001000 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +01001001
1002 switch (msg->cmd) {
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001003 case TIPC_CMD_NOOP:
1004 msg->rep = tipc_tlv_alloc(0);
1005 if (!msg->rep)
1006 return -ENOMEM;
1007 return 0;
Richard Alped0796d12015-02-09 09:50:04 +01001008 case TIPC_CMD_GET_BEARER_NAMES:
1009 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
1010 dump.dumpit = tipc_nl_bearer_dump;
1011 dump.format = tipc_nl_compat_bearer_dump;
1012 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +01001013 case TIPC_CMD_ENABLE_BEARER:
1014 msg->req_type = TIPC_TLV_BEARER_CONFIG;
1015 doit.doit = tipc_nl_bearer_enable;
1016 doit.transcode = tipc_nl_compat_bearer_enable;
1017 return tipc_nl_compat_doit(&doit, msg);
1018 case TIPC_CMD_DISABLE_BEARER:
1019 msg->req_type = TIPC_TLV_BEARER_NAME;
1020 doit.doit = tipc_nl_bearer_disable;
1021 doit.transcode = tipc_nl_compat_bearer_disable;
1022 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d42015-02-09 09:50:06 +01001023 case TIPC_CMD_SHOW_LINK_STATS:
1024 msg->req_type = TIPC_TLV_LINK_NAME;
1025 msg->rep_size = ULTRA_STRING_MAX_LEN;
1026 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1027 dump.dumpit = tipc_nl_link_dump;
1028 dump.format = tipc_nl_compat_link_stat_dump;
1029 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +01001030 case TIPC_CMD_GET_LINKS:
1031 msg->req_type = TIPC_TLV_NET_ADDR;
1032 msg->rep_size = ULTRA_STRING_MAX_LEN;
1033 dump.dumpit = tipc_nl_link_dump;
1034 dump.format = tipc_nl_compat_link_dump;
1035 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +01001036 case TIPC_CMD_SET_LINK_TOL:
1037 case TIPC_CMD_SET_LINK_PRI:
1038 case TIPC_CMD_SET_LINK_WINDOW:
1039 msg->req_type = TIPC_TLV_LINK_CONFIG;
1040 doit.doit = tipc_nl_link_set;
1041 doit.transcode = tipc_nl_compat_link_set;
1042 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +01001043 case TIPC_CMD_RESET_LINK_STATS:
1044 msg->req_type = TIPC_TLV_LINK_NAME;
1045 doit.doit = tipc_nl_link_reset_stats;
1046 doit.transcode = tipc_nl_compat_link_reset_stats;
1047 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +01001048 case TIPC_CMD_SHOW_NAME_TABLE:
1049 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
1050 msg->rep_size = ULTRA_STRING_MAX_LEN;
1051 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1052 dump.header = tipc_nl_compat_name_table_dump_header;
1053 dump.dumpit = tipc_nl_name_table_dump;
1054 dump.format = tipc_nl_compat_name_table_dump;
1055 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +01001056 case TIPC_CMD_SHOW_PORTS:
1057 msg->rep_size = ULTRA_STRING_MAX_LEN;
1058 msg->rep_type = TIPC_TLV_ULTRA_STRING;
1059 dump.dumpit = tipc_nl_sk_dump;
1060 dump.format = tipc_nl_compat_sk_dump;
1061 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +01001062 case TIPC_CMD_GET_MEDIA_NAMES:
1063 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
1064 dump.dumpit = tipc_nl_media_dump;
1065 dump.format = tipc_nl_compat_media_dump;
1066 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +01001067 case TIPC_CMD_GET_NODES:
1068 msg->rep_size = ULTRA_STRING_MAX_LEN;
1069 dump.dumpit = tipc_nl_node_dump;
1070 dump.format = tipc_nl_compat_node_dump;
1071 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +01001072 case TIPC_CMD_SET_NODE_ADDR:
1073 msg->req_type = TIPC_TLV_NET_ADDR;
1074 doit.doit = tipc_nl_net_set;
1075 doit.transcode = tipc_nl_compat_net_set;
1076 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +01001077 case TIPC_CMD_SET_NETID:
1078 msg->req_type = TIPC_TLV_UNSIGNED;
1079 doit.doit = tipc_nl_net_set;
1080 doit.transcode = tipc_nl_compat_net_set;
1081 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +01001082 case TIPC_CMD_GET_NETID:
1083 msg->rep_size = sizeof(u32);
1084 dump.dumpit = tipc_nl_net_dump;
1085 dump.format = tipc_nl_compat_net_dump;
1086 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +01001087 case TIPC_CMD_SHOW_STATS:
1088 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +01001089 }
1090
1091 return -EOPNOTSUPP;
1092}
1093
1094static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
1095{
1096 int err;
1097 int len;
1098 struct tipc_nl_compat_msg msg;
1099 struct nlmsghdr *req_nlh;
1100 struct nlmsghdr *rep_nlh;
1101 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
Richard Alped0796d12015-02-09 09:50:04 +01001102
1103 memset(&msg, 0, sizeof(msg));
1104
1105 req_nlh = (struct nlmsghdr *)skb->data;
1106 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1107 msg.cmd = req_userhdr->cmd;
1108 msg.dst_sk = info->dst_sk;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001109 msg.net = genl_info_net(info);
Richard Alped0796d12015-02-09 09:50:04 +01001110
1111 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1112 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1113 err = -EACCES;
1114 goto send;
1115 }
1116
1117 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
Richard Alpe8f8ff9132015-08-17 14:15:10 +02001118 if (len && !TLV_OK(msg.req, len)) {
Richard Alped0796d12015-02-09 09:50:04 +01001119 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1120 err = -EOPNOTSUPP;
1121 goto send;
1122 }
1123
1124 err = tipc_nl_compat_handle(&msg);
Richard Alpeb063bc52015-05-06 13:58:56 +02001125 if ((err == -EOPNOTSUPP) || (err == -EPERM))
Richard Alped0796d12015-02-09 09:50:04 +01001126 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1127 else if (err == -EINVAL)
1128 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1129send:
1130 if (!msg.rep)
1131 return err;
1132
1133 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1134 skb_push(msg.rep, len);
1135 rep_nlh = nlmsg_hdr(msg.rep);
1136 memcpy(rep_nlh, info->nlhdr, len);
1137 rep_nlh->nlmsg_len = msg.rep->len;
Richard Alpec3d6fb82015-05-06 13:58:54 +02001138 genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid);
Richard Alped0796d12015-02-09 09:50:04 +01001139
1140 return err;
1141}
1142
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001143static struct genl_family tipc_genl_compat_family = {
1144 .id = GENL_ID_GENERATE,
1145 .name = TIPC_GENL_NAME,
1146 .version = TIPC_GENL_VERSION,
1147 .hdrsize = TIPC_GENL_HDRLEN,
1148 .maxattr = 0,
1149 .netnsok = true,
1150};
1151
1152static struct genl_ops tipc_genl_compat_ops[] = {
1153 {
1154 .cmd = TIPC_GENL_CMD,
Richard Alpe22ae7cf2015-02-09 09:50:18 +01001155 .doit = tipc_nl_compat_recv,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001156 },
1157};
1158
1159int tipc_netlink_compat_start(void)
1160{
1161 int res;
1162
1163 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1164 tipc_genl_compat_ops);
1165 if (res) {
1166 pr_err("Failed to register legacy compat interface\n");
1167 return res;
1168 }
1169
1170 return 0;
1171}
1172
1173void tipc_netlink_compat_stop(void)
1174{
1175 genl_unregister_family(&tipc_genl_compat_family);
1176}