blob: 40c24ea31231a4c35a7fb751799b5193ea352e0c [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"
35#include "config.h"
Richard Alped0796d12015-02-09 09:50:04 +010036#include "bearer.h"
Richard Alpef2b3b2d2015-02-09 09:50:06 +010037#include "link.h"
Richard Alpe44a8ae92015-02-09 09:50:10 +010038#include "name_table.h"
Richard Alpebfb3e5d2015-02-09 09:50:03 +010039#include <net/genetlink.h>
40#include <linux/tipc_config.h>
41
Richard Alped0796d12015-02-09 09:50:04 +010042/* The legacy API had an artificial message length limit called
43 * ULTRA_STRING_MAX_LEN.
44 */
45#define ULTRA_STRING_MAX_LEN 32768
46
47#define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
48
49#define REPLY_TRUNCATED "<truncated>\n"
50
51struct tipc_nl_compat_msg {
52 u16 cmd;
Richard Alpef2b3b2d2015-02-09 09:50:06 +010053 int rep_type;
Richard Alped0796d12015-02-09 09:50:04 +010054 int rep_size;
Richard Alpe9ab15462015-02-09 09:50:05 +010055 int req_type;
Richard Alped0796d12015-02-09 09:50:04 +010056 struct sk_buff *rep;
57 struct tlv_desc *req;
58 struct sock *dst_sk;
59};
60
61struct tipc_nl_compat_cmd_dump {
Richard Alpe44a8ae92015-02-09 09:50:10 +010062 int (*header)(struct tipc_nl_compat_msg *);
Richard Alped0796d12015-02-09 09:50:04 +010063 int (*dumpit)(struct sk_buff *, struct netlink_callback *);
64 int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs);
65};
66
Richard Alpe9ab15462015-02-09 09:50:05 +010067struct tipc_nl_compat_cmd_doit {
68 int (*doit)(struct sk_buff *skb, struct genl_info *info);
69 int (*transcode)(struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
70};
71
Richard Alped0796d12015-02-09 09:50:04 +010072static int tipc_skb_tailroom(struct sk_buff *skb)
73{
74 int tailroom;
75 int limit;
76
77 tailroom = skb_tailroom(skb);
78 limit = TIPC_SKB_MAX - skb->len;
79
80 if (tailroom < limit)
81 return tailroom;
82
83 return limit;
84}
85
86static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
87{
88 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
89
90 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
91 return -EMSGSIZE;
92
93 skb_put(skb, TLV_SPACE(len));
94 tlv->tlv_type = htons(type);
95 tlv->tlv_len = htons(TLV_LENGTH(len));
96 if (len && data)
97 memcpy(TLV_DATA(tlv), data, len);
98
99 return 0;
100}
101
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100102static void tipc_tlv_init(struct sk_buff *skb, u16 type)
103{
104 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
105
106 TLV_SET_LEN(tlv, 0);
107 TLV_SET_TYPE(tlv, type);
108 skb_put(skb, sizeof(struct tlv_desc));
109}
110
111static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
112{
113 int n;
114 u16 len;
115 u32 rem;
116 char *buf;
117 struct tlv_desc *tlv;
118 va_list args;
119
120 rem = tipc_skb_tailroom(skb);
121
122 tlv = (struct tlv_desc *)skb->data;
123 len = TLV_GET_LEN(tlv);
124 buf = TLV_DATA(tlv) + len;
125
126 va_start(args, fmt);
127 n = vscnprintf(buf, rem, fmt, args);
128 va_end(args);
129
130 TLV_SET_LEN(tlv, n + len);
131 skb_put(skb, n);
132
133 return n;
134}
135
Richard Alped0796d12015-02-09 09:50:04 +0100136static struct sk_buff *tipc_tlv_alloc(int size)
137{
138 int hdr_len;
139 struct sk_buff *buf;
140
141 size = TLV_SPACE(size);
142 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
143
144 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
145 if (!buf)
146 return NULL;
147
148 skb_reserve(buf, hdr_len);
149
150 return buf;
151}
152
153static struct sk_buff *tipc_get_err_tlv(char *str)
154{
155 int str_len = strlen(str) + 1;
156 struct sk_buff *buf;
157
158 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
159 if (buf)
160 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
161
162 return buf;
163}
164
165static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
166 struct tipc_nl_compat_msg *msg,
167 struct sk_buff *arg)
168{
169 int len = 0;
170 int err;
171 struct sk_buff *buf;
172 struct nlmsghdr *nlmsg;
173 struct netlink_callback cb;
174
175 memset(&cb, 0, sizeof(cb));
176 cb.nlh = (struct nlmsghdr *)arg->data;
177 cb.skb = arg;
178
179 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
180 if (!buf)
181 return -ENOMEM;
182
183 buf->sk = msg->dst_sk;
184
185 do {
186 int rem;
187
188 len = (*cmd->dumpit)(buf, &cb);
189
190 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
191 struct nlattr **attrs;
192
193 err = tipc_nlmsg_parse(nlmsg, &attrs);
194 if (err)
195 goto err_out;
196
197 err = (*cmd->format)(msg, attrs);
198 if (err)
199 goto err_out;
200
201 if (tipc_skb_tailroom(msg->rep) <= 1) {
202 err = -EMSGSIZE;
203 goto err_out;
204 }
205 }
206
207 skb_reset_tail_pointer(buf);
208 buf->len = 0;
209
210 } while (len);
211
212 err = 0;
213
214err_out:
215 kfree_skb(buf);
216
217 if (err == -EMSGSIZE) {
218 /* The legacy API only considered messages filling
219 * "ULTRA_STRING_MAX_LEN" to be truncated.
220 */
221 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
222 char *tail = skb_tail_pointer(msg->rep);
223
224 if (*tail != '\0')
225 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
226 REPLY_TRUNCATED);
227 }
228
229 return 0;
230 }
231
232 return err;
233}
234
235static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
236 struct tipc_nl_compat_msg *msg)
237{
238 int err;
239 struct sk_buff *arg;
240
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100241 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
242 return -EINVAL;
243
Richard Alped0796d12015-02-09 09:50:04 +0100244 msg->rep = tipc_tlv_alloc(msg->rep_size);
245 if (!msg->rep)
246 return -ENOMEM;
247
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100248 if (msg->rep_type)
249 tipc_tlv_init(msg->rep, msg->rep_type);
250
Richard Alpe44a8ae92015-02-09 09:50:10 +0100251 if (cmd->header)
252 (*cmd->header)(msg);
253
Richard Alped0796d12015-02-09 09:50:04 +0100254 arg = nlmsg_new(0, GFP_KERNEL);
255 if (!arg) {
256 kfree_skb(msg->rep);
257 return -ENOMEM;
258 }
259
260 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
261 if (err)
262 kfree_skb(msg->rep);
263
264 kfree_skb(arg);
265
266 return err;
267}
268
Richard Alpe9ab15462015-02-09 09:50:05 +0100269static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
270 struct tipc_nl_compat_msg *msg)
271{
272 int err;
273 struct sk_buff *doit_buf;
274 struct sk_buff *trans_buf;
275 struct nlattr **attrbuf;
276 struct genl_info info;
277
278 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
279 if (!trans_buf)
280 return -ENOMEM;
281
282 err = (*cmd->transcode)(trans_buf, msg);
283 if (err)
284 goto trans_out;
285
286 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
287 sizeof(struct nlattr *), GFP_KERNEL);
288 if (!attrbuf) {
289 err = -ENOMEM;
290 goto trans_out;
291 }
292
293 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
294 (const struct nlattr *)trans_buf->data,
295 trans_buf->len, NULL);
296 if (err)
297 goto parse_out;
298
299 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
300 if (!doit_buf) {
301 err = -ENOMEM;
302 goto parse_out;
303 }
304
305 doit_buf->sk = msg->dst_sk;
306
307 memset(&info, 0, sizeof(info));
308 info.attrs = attrbuf;
309
310 err = (*cmd->doit)(doit_buf, &info);
311
312 kfree_skb(doit_buf);
313parse_out:
314 kfree(attrbuf);
315trans_out:
316 kfree_skb(trans_buf);
317
318 return err;
319}
320
321static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
322 struct tipc_nl_compat_msg *msg)
323{
324 int err;
325
326 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
327 return -EINVAL;
328
329 err = __tipc_nl_compat_doit(cmd, msg);
330 if (err)
331 return err;
332
333 /* The legacy API considered an empty message a success message */
334 msg->rep = tipc_tlv_alloc(0);
335 if (!msg->rep)
336 return -ENOMEM;
337
338 return 0;
339}
340
Richard Alped0796d12015-02-09 09:50:04 +0100341static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
342 struct nlattr **attrs)
343{
344 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
345
346 nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
347 NULL);
348
349 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
350 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
351 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
352}
353
Richard Alpe9ab15462015-02-09 09:50:05 +0100354static int tipc_nl_compat_bearer_enable(struct sk_buff *skb,
355 struct tipc_nl_compat_msg *msg)
356{
357 struct nlattr *prop;
358 struct nlattr *bearer;
359 struct tipc_bearer_config *b;
360
361 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
362
363 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
364 if (!bearer)
365 return -EMSGSIZE;
366
367 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
368 return -EMSGSIZE;
369
370 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
371 return -EMSGSIZE;
372
373 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
374 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
375 if (!prop)
376 return -EMSGSIZE;
377 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
378 return -EMSGSIZE;
379 nla_nest_end(skb, prop);
380 }
381 nla_nest_end(skb, bearer);
382
383 return 0;
384}
385
386static int tipc_nl_compat_bearer_disable(struct sk_buff *skb,
387 struct tipc_nl_compat_msg *msg)
388{
389 char *name;
390 struct nlattr *bearer;
391
392 name = (char *)TLV_DATA(msg->req);
393
394 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
395 if (!bearer)
396 return -EMSGSIZE;
397
398 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
399 return -EMSGSIZE;
400
401 nla_nest_end(skb, bearer);
402
403 return 0;
404}
405
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100406static inline u32 perc(u32 count, u32 total)
407{
408 return (count * 100 + (total / 2)) / total;
409}
410
411static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
412 struct nlattr *prop[], struct nlattr *stats[])
413{
414 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
415 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
416
417 tipc_tlv_sprintf(msg->rep,
418 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
419 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
420 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
421 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
422 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
423 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
424
425 tipc_tlv_sprintf(msg->rep,
426 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
427 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
428 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
429 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
430 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
431 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
432
433 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
434 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
435 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
436 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
437
438 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
439 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
440 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
441 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
442
443 tipc_tlv_sprintf(msg->rep,
444 " Congestion link:%u Send queue max:%u avg:%u",
445 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
446 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
447 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
448}
449
450static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
451 struct nlattr **attrs)
452{
453 char *name;
454 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
455 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
456 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
457
458 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
459
460 nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
461 NULL);
462
463 nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
464 NULL);
465
466 name = (char *)TLV_DATA(msg->req);
467 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
468 return 0;
469
470 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
471 nla_data(link[TIPC_NLA_LINK_NAME]));
472
473 if (link[TIPC_NLA_LINK_BROADCAST]) {
474 __fill_bc_link_stat(msg, prop, stats);
475 return 0;
476 }
477
478 if (link[TIPC_NLA_LINK_ACTIVE])
479 tipc_tlv_sprintf(msg->rep, " ACTIVE");
480 else if (link[TIPC_NLA_LINK_UP])
481 tipc_tlv_sprintf(msg->rep, " STANDBY");
482 else
483 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
484
485 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
486 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
487 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
488
489 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
490 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
491 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
492
493 tipc_tlv_sprintf(msg->rep,
494 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
495 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
496 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
497 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
498 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
499 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
500 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
501
502 tipc_tlv_sprintf(msg->rep,
503 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
504 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
505 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
506 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
507 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
508 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
509 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
510
511 tipc_tlv_sprintf(msg->rep,
512 " TX profile sample:%u packets average:%u octets\n",
513 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
514 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
515 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
516
517 tipc_tlv_sprintf(msg->rep,
518 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
519 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
520 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
521 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
522 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
523 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
524 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
525 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
526 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
527
528 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
529 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
530 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
531 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
532 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
533 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
534 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
535
536 tipc_tlv_sprintf(msg->rep,
537 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
538 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
539 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
540 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
541 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
542 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
543
544 tipc_tlv_sprintf(msg->rep,
545 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
546 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
547 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
548 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
549 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
550 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
551
552 tipc_tlv_sprintf(msg->rep,
553 " Congestion link:%u Send queue max:%u avg:%u",
554 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
555 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
556 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
557
558 return 0;
559}
560
Richard Alpe357ebdb2015-02-09 09:50:07 +0100561static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
562 struct nlattr **attrs)
563{
564 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
565 struct tipc_link_info link_info;
566
567 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
568
569 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
570 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
571 strcpy(link_info.str, nla_data(link[TIPC_NLA_LINK_NAME]));
572
573 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
574 &link_info, sizeof(link_info));
575}
576
Richard Alpe37e2d482015-02-09 09:50:08 +0100577static int tipc_nl_compat_link_set(struct sk_buff *skb,
578 struct tipc_nl_compat_msg *msg)
579{
580 struct nlattr *link;
581 struct nlattr *prop;
582 struct tipc_link_config *lc;
583
584 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
585
586 link = nla_nest_start(skb, TIPC_NLA_LINK);
587 if (!link)
588 return -EMSGSIZE;
589
590 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
591 return -EMSGSIZE;
592
593 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
594 if (!prop)
595 return -EMSGSIZE;
596
597 if (msg->cmd == TIPC_CMD_SET_LINK_PRI) {
598 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value)))
599 return -EMSGSIZE;
600 } else if (msg->cmd == TIPC_CMD_SET_LINK_TOL) {
601 if (nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value)))
602 return -EMSGSIZE;
603 } else if (msg->cmd == TIPC_CMD_SET_LINK_WINDOW) {
604 if (nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value)))
605 return -EMSGSIZE;
606 }
607
608 nla_nest_end(skb, prop);
609 nla_nest_end(skb, link);
610
611 return 0;
612}
613
Richard Alpe18178772015-02-09 09:50:09 +0100614static int tipc_nl_compat_link_reset_stats(struct sk_buff *skb,
615 struct tipc_nl_compat_msg *msg)
616{
617 char *name;
618 struct nlattr *link;
619
620 name = (char *)TLV_DATA(msg->req);
621
622 link = nla_nest_start(skb, TIPC_NLA_LINK);
623 if (!link)
624 return -EMSGSIZE;
625
626 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
627 return -EMSGSIZE;
628
629 nla_nest_end(skb, link);
630
631 return 0;
632}
633
Richard Alpe44a8ae92015-02-09 09:50:10 +0100634static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
635{
636 int i;
637 u32 depth;
638 struct tipc_name_table_query *ntq;
639 static const char * const header[] = {
640 "Type ",
641 "Lower Upper ",
642 "Port Identity ",
643 "Publication Scope"
644 };
645
646 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
647
648 depth = ntohl(ntq->depth);
649
650 if (depth > 4)
651 depth = 4;
652 for (i = 0; i < depth; i++)
653 tipc_tlv_sprintf(msg->rep, header[i]);
654 tipc_tlv_sprintf(msg->rep, "\n");
655
656 return 0;
657}
658
659static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
660 struct nlattr **attrs)
661{
662 char port_str[27];
663 struct tipc_name_table_query *ntq;
664 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
665 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
666 u32 node, depth, type, lowbound, upbound;
667 static const char * const scope_str[] = {"", " zone", " cluster",
668 " node"};
669
670 nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
671 attrs[TIPC_NLA_NAME_TABLE], NULL);
672
673 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, nt[TIPC_NLA_NAME_TABLE_PUBL],
674 NULL);
675
676 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
677
678 depth = ntohl(ntq->depth);
679 type = ntohl(ntq->type);
680 lowbound = ntohl(ntq->lowbound);
681 upbound = ntohl(ntq->upbound);
682
683 if (!(depth & TIPC_NTQ_ALLTYPES) &&
684 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
685 return 0;
686 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
687 return 0;
688 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
689 return 0;
690
691 tipc_tlv_sprintf(msg->rep, "%-10u ",
692 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
693
694 if (depth == 1)
695 goto out;
696
697 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
698 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
699 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
700
701 if (depth == 2)
702 goto out;
703
704 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
705 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
706 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
707 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
708
709 if (depth == 3)
710 goto out;
711
712 tipc_tlv_sprintf(msg->rep, "%-10u %s",
713 nla_get_u32(publ[TIPC_NLA_PUBL_REF]),
714 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
715out:
716 tipc_tlv_sprintf(msg->rep, "\n");
717
718 return 0;
719}
720
Richard Alped0796d12015-02-09 09:50:04 +0100721static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
722{
723 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +0100724 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +0100725
726 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +0100727 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +0100728
729 switch (msg->cmd) {
730 case TIPC_CMD_GET_BEARER_NAMES:
731 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
732 dump.dumpit = tipc_nl_bearer_dump;
733 dump.format = tipc_nl_compat_bearer_dump;
734 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100735 case TIPC_CMD_ENABLE_BEARER:
736 msg->req_type = TIPC_TLV_BEARER_CONFIG;
737 doit.doit = tipc_nl_bearer_enable;
738 doit.transcode = tipc_nl_compat_bearer_enable;
739 return tipc_nl_compat_doit(&doit, msg);
740 case TIPC_CMD_DISABLE_BEARER:
741 msg->req_type = TIPC_TLV_BEARER_NAME;
742 doit.doit = tipc_nl_bearer_disable;
743 doit.transcode = tipc_nl_compat_bearer_disable;
744 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100745 case TIPC_CMD_SHOW_LINK_STATS:
746 msg->req_type = TIPC_TLV_LINK_NAME;
747 msg->rep_size = ULTRA_STRING_MAX_LEN;
748 msg->rep_type = TIPC_TLV_ULTRA_STRING;
749 dump.dumpit = tipc_nl_link_dump;
750 dump.format = tipc_nl_compat_link_stat_dump;
751 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100752 case TIPC_CMD_GET_LINKS:
753 msg->req_type = TIPC_TLV_NET_ADDR;
754 msg->rep_size = ULTRA_STRING_MAX_LEN;
755 dump.dumpit = tipc_nl_link_dump;
756 dump.format = tipc_nl_compat_link_dump;
757 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +0100758 case TIPC_CMD_SET_LINK_TOL:
759 case TIPC_CMD_SET_LINK_PRI:
760 case TIPC_CMD_SET_LINK_WINDOW:
761 msg->req_type = TIPC_TLV_LINK_CONFIG;
762 doit.doit = tipc_nl_link_set;
763 doit.transcode = tipc_nl_compat_link_set;
764 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +0100765 case TIPC_CMD_RESET_LINK_STATS:
766 msg->req_type = TIPC_TLV_LINK_NAME;
767 doit.doit = tipc_nl_link_reset_stats;
768 doit.transcode = tipc_nl_compat_link_reset_stats;
769 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +0100770 case TIPC_CMD_SHOW_NAME_TABLE:
771 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
772 msg->rep_size = ULTRA_STRING_MAX_LEN;
773 msg->rep_type = TIPC_TLV_ULTRA_STRING;
774 dump.header = tipc_nl_compat_name_table_dump_header;
775 dump.dumpit = tipc_nl_name_table_dump;
776 dump.format = tipc_nl_compat_name_table_dump;
777 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped0796d12015-02-09 09:50:04 +0100778 }
779
780 return -EOPNOTSUPP;
781}
782
783static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
784{
785 int err;
786 int len;
787 struct tipc_nl_compat_msg msg;
788 struct nlmsghdr *req_nlh;
789 struct nlmsghdr *rep_nlh;
790 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
791 struct net *net = genl_info_net(info);
792
793 memset(&msg, 0, sizeof(msg));
794
795 req_nlh = (struct nlmsghdr *)skb->data;
796 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
797 msg.cmd = req_userhdr->cmd;
798 msg.dst_sk = info->dst_sk;
799
800 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
801 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
802 err = -EACCES;
803 goto send;
804 }
805
806 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
807 if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
808 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
809 err = -EOPNOTSUPP;
810 goto send;
811 }
812
813 err = tipc_nl_compat_handle(&msg);
814 if (err == -EOPNOTSUPP)
815 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
816 else if (err == -EINVAL)
817 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
818send:
819 if (!msg.rep)
820 return err;
821
822 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
823 skb_push(msg.rep, len);
824 rep_nlh = nlmsg_hdr(msg.rep);
825 memcpy(rep_nlh, info->nlhdr, len);
826 rep_nlh->nlmsg_len = msg.rep->len;
827 genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
828
829 return err;
830}
831
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100832static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
833{
834 struct net *net = genl_info_net(info);
835 struct sk_buff *rep_buf;
836 struct nlmsghdr *rep_nlh;
837 struct nlmsghdr *req_nlh = info->nlhdr;
838 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
839 int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
840 u16 cmd;
841
842 if ((req_userhdr->cmd & 0xC000) &&
843 (!netlink_net_capable(skb, CAP_NET_ADMIN)))
844 cmd = TIPC_CMD_NOT_NET_ADMIN;
845 else
846 cmd = req_userhdr->cmd;
847
848 rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
849 nlmsg_data(req_nlh) + GENL_HDRLEN +
850 TIPC_GENL_HDRLEN,
851 nlmsg_attrlen(req_nlh, GENL_HDRLEN +
852 TIPC_GENL_HDRLEN), hdr_space);
853
854 if (rep_buf) {
855 skb_push(rep_buf, hdr_space);
856 rep_nlh = nlmsg_hdr(rep_buf);
857 memcpy(rep_nlh, req_nlh, hdr_space);
858 rep_nlh->nlmsg_len = rep_buf->len;
859 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
860 }
861
862 return 0;
863}
864
Richard Alped0796d12015-02-09 09:50:04 +0100865/* Temporary function to keep functionality throughout the patchset
866 * without having to mess with the global variables and other trickery
867 * of the old API.
868 */
869static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
870{
871 struct tipc_genlmsghdr *req = info->userhdr;
872
873 switch (req->cmd) {
874 case TIPC_CMD_GET_BEARER_NAMES:
Richard Alpe9ab15462015-02-09 09:50:05 +0100875 case TIPC_CMD_ENABLE_BEARER:
876 case TIPC_CMD_DISABLE_BEARER:
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100877 case TIPC_CMD_SHOW_LINK_STATS:
Richard Alpe357ebdb2015-02-09 09:50:07 +0100878 case TIPC_CMD_GET_LINKS:
Richard Alpe37e2d482015-02-09 09:50:08 +0100879 case TIPC_CMD_SET_LINK_TOL:
880 case TIPC_CMD_SET_LINK_PRI:
881 case TIPC_CMD_SET_LINK_WINDOW:
Richard Alpe18178772015-02-09 09:50:09 +0100882 case TIPC_CMD_RESET_LINK_STATS:
Richard Alpe44a8ae92015-02-09 09:50:10 +0100883 case TIPC_CMD_SHOW_NAME_TABLE:
Richard Alped0796d12015-02-09 09:50:04 +0100884 return tipc_nl_compat_recv(skb, info);
885 }
886
887 return handle_cmd(skb, info);
888}
889
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100890static struct genl_family tipc_genl_compat_family = {
891 .id = GENL_ID_GENERATE,
892 .name = TIPC_GENL_NAME,
893 .version = TIPC_GENL_VERSION,
894 .hdrsize = TIPC_GENL_HDRLEN,
895 .maxattr = 0,
896 .netnsok = true,
897};
898
899static struct genl_ops tipc_genl_compat_ops[] = {
900 {
901 .cmd = TIPC_GENL_CMD,
Richard Alped0796d12015-02-09 09:50:04 +0100902 .doit = tipc_nl_compat_tmp_wrap,
Richard Alpebfb3e5d2015-02-09 09:50:03 +0100903 },
904};
905
906int tipc_netlink_compat_start(void)
907{
908 int res;
909
910 res = genl_register_family_with_ops(&tipc_genl_compat_family,
911 tipc_genl_compat_ops);
912 if (res) {
913 pr_err("Failed to register legacy compat interface\n");
914 return res;
915 }
916
917 return 0;
918}
919
920void tipc_netlink_compat_stop(void)
921{
922 genl_unregister_family(&tipc_genl_compat_family);
923}