blob: cb9086d259df5b739acfb2813463d0eeb11490e6 [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 Alpe487d2a32015-02-09 09:50:11 +010039#include "socket.h"
Richard Alpe4b28cb52015-02-09 09:50:13 +010040#include "node.h"
Richard Alpe964f9502015-02-09 09:50:15 +010041#include "net.h"
Richard Alpebfb3e5d2015-02-09 09:50:03 +010042#include <net/genetlink.h>
43#include <linux/tipc_config.h>
44
Richard Alped0796d12015-02-09 09:50:04 +010045/* The legacy API had an artificial message length limit called
46 * ULTRA_STRING_MAX_LEN.
47 */
48#define ULTRA_STRING_MAX_LEN 32768
49
50#define TIPC_SKB_MAX TLV_SPACE(ULTRA_STRING_MAX_LEN)
51
52#define REPLY_TRUNCATED "<truncated>\n"
53
54struct tipc_nl_compat_msg {
55 u16 cmd;
Richard Alpef2b3b2d2015-02-09 09:50:06 +010056 int rep_type;
Richard Alped0796d12015-02-09 09:50:04 +010057 int rep_size;
Richard Alpe9ab15462015-02-09 09:50:05 +010058 int req_type;
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);
72 int (*transcode)(struct sk_buff *skb, struct tipc_nl_compat_msg *msg);
73};
74
Richard Alped0796d12015-02-09 09:50:04 +010075static int tipc_skb_tailroom(struct sk_buff *skb)
76{
77 int tailroom;
78 int limit;
79
80 tailroom = skb_tailroom(skb);
81 limit = TIPC_SKB_MAX - skb->len;
82
83 if (tailroom < limit)
84 return tailroom;
85
86 return limit;
87}
88
89static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len)
90{
91 struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb);
92
93 if (tipc_skb_tailroom(skb) < TLV_SPACE(len))
94 return -EMSGSIZE;
95
96 skb_put(skb, TLV_SPACE(len));
97 tlv->tlv_type = htons(type);
98 tlv->tlv_len = htons(TLV_LENGTH(len));
99 if (len && data)
100 memcpy(TLV_DATA(tlv), data, len);
101
102 return 0;
103}
104
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100105static void tipc_tlv_init(struct sk_buff *skb, u16 type)
106{
107 struct tlv_desc *tlv = (struct tlv_desc *)skb->data;
108
109 TLV_SET_LEN(tlv, 0);
110 TLV_SET_TYPE(tlv, type);
111 skb_put(skb, sizeof(struct tlv_desc));
112}
113
114static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...)
115{
116 int n;
117 u16 len;
118 u32 rem;
119 char *buf;
120 struct tlv_desc *tlv;
121 va_list args;
122
123 rem = tipc_skb_tailroom(skb);
124
125 tlv = (struct tlv_desc *)skb->data;
126 len = TLV_GET_LEN(tlv);
127 buf = TLV_DATA(tlv) + len;
128
129 va_start(args, fmt);
130 n = vscnprintf(buf, rem, fmt, args);
131 va_end(args);
132
133 TLV_SET_LEN(tlv, n + len);
134 skb_put(skb, n);
135
136 return n;
137}
138
Richard Alped0796d12015-02-09 09:50:04 +0100139static struct sk_buff *tipc_tlv_alloc(int size)
140{
141 int hdr_len;
142 struct sk_buff *buf;
143
144 size = TLV_SPACE(size);
145 hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
146
147 buf = alloc_skb(hdr_len + size, GFP_KERNEL);
148 if (!buf)
149 return NULL;
150
151 skb_reserve(buf, hdr_len);
152
153 return buf;
154}
155
156static struct sk_buff *tipc_get_err_tlv(char *str)
157{
158 int str_len = strlen(str) + 1;
159 struct sk_buff *buf;
160
161 buf = tipc_tlv_alloc(TLV_SPACE(str_len));
162 if (buf)
163 tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len);
164
165 return buf;
166}
167
168static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
169 struct tipc_nl_compat_msg *msg,
170 struct sk_buff *arg)
171{
172 int len = 0;
173 int err;
174 struct sk_buff *buf;
175 struct nlmsghdr *nlmsg;
176 struct netlink_callback cb;
177
178 memset(&cb, 0, sizeof(cb));
179 cb.nlh = (struct nlmsghdr *)arg->data;
180 cb.skb = arg;
181
182 buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
183 if (!buf)
184 return -ENOMEM;
185
186 buf->sk = msg->dst_sk;
187
188 do {
189 int rem;
190
191 len = (*cmd->dumpit)(buf, &cb);
192
193 nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) {
194 struct nlattr **attrs;
195
196 err = tipc_nlmsg_parse(nlmsg, &attrs);
197 if (err)
198 goto err_out;
199
200 err = (*cmd->format)(msg, attrs);
201 if (err)
202 goto err_out;
203
204 if (tipc_skb_tailroom(msg->rep) <= 1) {
205 err = -EMSGSIZE;
206 goto err_out;
207 }
208 }
209
210 skb_reset_tail_pointer(buf);
211 buf->len = 0;
212
213 } while (len);
214
215 err = 0;
216
217err_out:
218 kfree_skb(buf);
219
220 if (err == -EMSGSIZE) {
221 /* The legacy API only considered messages filling
222 * "ULTRA_STRING_MAX_LEN" to be truncated.
223 */
224 if ((TIPC_SKB_MAX - msg->rep->len) <= 1) {
225 char *tail = skb_tail_pointer(msg->rep);
226
227 if (*tail != '\0')
228 sprintf(tail - sizeof(REPLY_TRUNCATED) - 1,
229 REPLY_TRUNCATED);
230 }
231
232 return 0;
233 }
234
235 return err;
236}
237
238static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,
239 struct tipc_nl_compat_msg *msg)
240{
241 int err;
242 struct sk_buff *arg;
243
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100244 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
245 return -EINVAL;
246
Richard Alped0796d12015-02-09 09:50:04 +0100247 msg->rep = tipc_tlv_alloc(msg->rep_size);
248 if (!msg->rep)
249 return -ENOMEM;
250
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100251 if (msg->rep_type)
252 tipc_tlv_init(msg->rep, msg->rep_type);
253
Richard Alpe44a8ae92015-02-09 09:50:10 +0100254 if (cmd->header)
255 (*cmd->header)(msg);
256
Richard Alped0796d12015-02-09 09:50:04 +0100257 arg = nlmsg_new(0, GFP_KERNEL);
258 if (!arg) {
259 kfree_skb(msg->rep);
260 return -ENOMEM;
261 }
262
263 err = __tipc_nl_compat_dumpit(cmd, msg, arg);
264 if (err)
265 kfree_skb(msg->rep);
266
267 kfree_skb(arg);
268
269 return err;
270}
271
Richard Alpe9ab15462015-02-09 09:50:05 +0100272static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
273 struct tipc_nl_compat_msg *msg)
274{
275 int err;
276 struct sk_buff *doit_buf;
277 struct sk_buff *trans_buf;
278 struct nlattr **attrbuf;
279 struct genl_info info;
280
281 trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
282 if (!trans_buf)
283 return -ENOMEM;
284
285 err = (*cmd->transcode)(trans_buf, msg);
286 if (err)
287 goto trans_out;
288
289 attrbuf = kmalloc((tipc_genl_family.maxattr + 1) *
290 sizeof(struct nlattr *), GFP_KERNEL);
291 if (!attrbuf) {
292 err = -ENOMEM;
293 goto trans_out;
294 }
295
296 err = nla_parse(attrbuf, tipc_genl_family.maxattr,
297 (const struct nlattr *)trans_buf->data,
298 trans_buf->len, NULL);
299 if (err)
300 goto parse_out;
301
302 doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
303 if (!doit_buf) {
304 err = -ENOMEM;
305 goto parse_out;
306 }
307
308 doit_buf->sk = msg->dst_sk;
309
310 memset(&info, 0, sizeof(info));
311 info.attrs = attrbuf;
312
313 err = (*cmd->doit)(doit_buf, &info);
314
315 kfree_skb(doit_buf);
316parse_out:
317 kfree(attrbuf);
318trans_out:
319 kfree_skb(trans_buf);
320
321 return err;
322}
323
324static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd,
325 struct tipc_nl_compat_msg *msg)
326{
327 int err;
328
329 if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type))
330 return -EINVAL;
331
332 err = __tipc_nl_compat_doit(cmd, msg);
333 if (err)
334 return err;
335
336 /* The legacy API considered an empty message a success message */
337 msg->rep = tipc_tlv_alloc(0);
338 if (!msg->rep)
339 return -ENOMEM;
340
341 return 0;
342}
343
Richard Alped0796d12015-02-09 09:50:04 +0100344static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg,
345 struct nlattr **attrs)
346{
347 struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1];
348
349 nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, attrs[TIPC_NLA_BEARER],
350 NULL);
351
352 return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME,
353 nla_data(bearer[TIPC_NLA_BEARER_NAME]),
354 nla_len(bearer[TIPC_NLA_BEARER_NAME]));
355}
356
Richard Alpe9ab15462015-02-09 09:50:05 +0100357static int tipc_nl_compat_bearer_enable(struct sk_buff *skb,
358 struct tipc_nl_compat_msg *msg)
359{
360 struct nlattr *prop;
361 struct nlattr *bearer;
362 struct tipc_bearer_config *b;
363
364 b = (struct tipc_bearer_config *)TLV_DATA(msg->req);
365
366 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
367 if (!bearer)
368 return -EMSGSIZE;
369
370 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name))
371 return -EMSGSIZE;
372
373 if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain)))
374 return -EMSGSIZE;
375
376 if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) {
377 prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP);
378 if (!prop)
379 return -EMSGSIZE;
380 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority)))
381 return -EMSGSIZE;
382 nla_nest_end(skb, prop);
383 }
384 nla_nest_end(skb, bearer);
385
386 return 0;
387}
388
389static int tipc_nl_compat_bearer_disable(struct sk_buff *skb,
390 struct tipc_nl_compat_msg *msg)
391{
392 char *name;
393 struct nlattr *bearer;
394
395 name = (char *)TLV_DATA(msg->req);
396
397 bearer = nla_nest_start(skb, TIPC_NLA_BEARER);
398 if (!bearer)
399 return -EMSGSIZE;
400
401 if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name))
402 return -EMSGSIZE;
403
404 nla_nest_end(skb, bearer);
405
406 return 0;
407}
408
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100409static inline u32 perc(u32 count, u32 total)
410{
411 return (count * 100 + (total / 2)) / total;
412}
413
414static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg,
415 struct nlattr *prop[], struct nlattr *stats[])
416{
417 tipc_tlv_sprintf(msg->rep, " Window:%u packets\n",
418 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
419
420 tipc_tlv_sprintf(msg->rep,
421 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
422 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
423 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
424 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
425 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
426 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
427
428 tipc_tlv_sprintf(msg->rep,
429 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
430 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
431 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
432 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
433 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
434 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
435
436 tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n",
437 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
438 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
439 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
440
441 tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n",
442 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
443 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
444 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
445
446 tipc_tlv_sprintf(msg->rep,
447 " Congestion link:%u Send queue max:%u avg:%u",
448 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
449 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
450 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
451}
452
453static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg,
454 struct nlattr **attrs)
455{
456 char *name;
457 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
458 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1];
459 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1];
460
461 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
462
463 nla_parse_nested(prop, TIPC_NLA_PROP_MAX, link[TIPC_NLA_LINK_PROP],
464 NULL);
465
466 nla_parse_nested(stats, TIPC_NLA_STATS_MAX, link[TIPC_NLA_LINK_STATS],
467 NULL);
468
469 name = (char *)TLV_DATA(msg->req);
470 if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0)
471 return 0;
472
473 tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n",
474 nla_data(link[TIPC_NLA_LINK_NAME]));
475
476 if (link[TIPC_NLA_LINK_BROADCAST]) {
477 __fill_bc_link_stat(msg, prop, stats);
478 return 0;
479 }
480
481 if (link[TIPC_NLA_LINK_ACTIVE])
482 tipc_tlv_sprintf(msg->rep, " ACTIVE");
483 else if (link[TIPC_NLA_LINK_UP])
484 tipc_tlv_sprintf(msg->rep, " STANDBY");
485 else
486 tipc_tlv_sprintf(msg->rep, " DEFUNCT");
487
488 tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u",
489 nla_get_u32(link[TIPC_NLA_LINK_MTU]),
490 nla_get_u32(prop[TIPC_NLA_PROP_PRIO]));
491
492 tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n",
493 nla_get_u32(prop[TIPC_NLA_PROP_TOL]),
494 nla_get_u32(prop[TIPC_NLA_PROP_WIN]));
495
496 tipc_tlv_sprintf(msg->rep,
497 " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
498 nla_get_u32(link[TIPC_NLA_LINK_RX]) -
499 nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]),
500 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]),
501 nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]),
502 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]),
503 nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
504
505 tipc_tlv_sprintf(msg->rep,
506 " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
507 nla_get_u32(link[TIPC_NLA_LINK_TX]) -
508 nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]),
509 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]),
510 nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]),
511 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]),
512 nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
513
514 tipc_tlv_sprintf(msg->rep,
515 " TX profile sample:%u packets average:%u octets\n",
516 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]),
517 nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) /
518 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]));
519
520 tipc_tlv_sprintf(msg->rep,
521 " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ",
522 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]),
523 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
524 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]),
525 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
526 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]),
527 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
528 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]),
529 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
530
531 tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n",
532 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]),
533 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
534 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]),
535 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])),
536 perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]),
537 nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])));
538
539 tipc_tlv_sprintf(msg->rep,
540 " RX states:%u probes:%u naks:%u defs:%u dups:%u\n",
541 nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]),
542 nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]),
543 nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]),
544 nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]),
545 nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
546
547 tipc_tlv_sprintf(msg->rep,
548 " TX states:%u probes:%u naks:%u acks:%u dups:%u\n",
549 nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]),
550 nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]),
551 nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]),
552 nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]),
553 nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
554
555 tipc_tlv_sprintf(msg->rep,
556 " Congestion link:%u Send queue max:%u avg:%u",
557 nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]),
558 nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]),
559 nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
560
561 return 0;
562}
563
Richard Alpe357ebdb2015-02-09 09:50:07 +0100564static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg,
565 struct nlattr **attrs)
566{
567 struct nlattr *link[TIPC_NLA_LINK_MAX + 1];
568 struct tipc_link_info link_info;
569
570 nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], NULL);
571
572 link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]);
573 link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP]));
574 strcpy(link_info.str, nla_data(link[TIPC_NLA_LINK_NAME]));
575
576 return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO,
577 &link_info, sizeof(link_info));
578}
579
Richard Alpe37e2d482015-02-09 09:50:08 +0100580static int tipc_nl_compat_link_set(struct sk_buff *skb,
581 struct tipc_nl_compat_msg *msg)
582{
583 struct nlattr *link;
584 struct nlattr *prop;
585 struct tipc_link_config *lc;
586
587 lc = (struct tipc_link_config *)TLV_DATA(msg->req);
588
589 link = nla_nest_start(skb, TIPC_NLA_LINK);
590 if (!link)
591 return -EMSGSIZE;
592
593 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name))
594 return -EMSGSIZE;
595
596 prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP);
597 if (!prop)
598 return -EMSGSIZE;
599
600 if (msg->cmd == TIPC_CMD_SET_LINK_PRI) {
601 if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value)))
602 return -EMSGSIZE;
603 } else if (msg->cmd == TIPC_CMD_SET_LINK_TOL) {
604 if (nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value)))
605 return -EMSGSIZE;
606 } else if (msg->cmd == TIPC_CMD_SET_LINK_WINDOW) {
607 if (nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value)))
608 return -EMSGSIZE;
609 }
610
611 nla_nest_end(skb, prop);
612 nla_nest_end(skb, link);
613
614 return 0;
615}
616
Richard Alpe18178772015-02-09 09:50:09 +0100617static int tipc_nl_compat_link_reset_stats(struct sk_buff *skb,
618 struct tipc_nl_compat_msg *msg)
619{
620 char *name;
621 struct nlattr *link;
622
623 name = (char *)TLV_DATA(msg->req);
624
625 link = nla_nest_start(skb, TIPC_NLA_LINK);
626 if (!link)
627 return -EMSGSIZE;
628
629 if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name))
630 return -EMSGSIZE;
631
632 nla_nest_end(skb, link);
633
634 return 0;
635}
636
Richard Alpe44a8ae92015-02-09 09:50:10 +0100637static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg)
638{
639 int i;
640 u32 depth;
641 struct tipc_name_table_query *ntq;
642 static const char * const header[] = {
643 "Type ",
644 "Lower Upper ",
645 "Port Identity ",
646 "Publication Scope"
647 };
648
649 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
650
651 depth = ntohl(ntq->depth);
652
653 if (depth > 4)
654 depth = 4;
655 for (i = 0; i < depth; i++)
656 tipc_tlv_sprintf(msg->rep, header[i]);
657 tipc_tlv_sprintf(msg->rep, "\n");
658
659 return 0;
660}
661
662static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg,
663 struct nlattr **attrs)
664{
665 char port_str[27];
666 struct tipc_name_table_query *ntq;
667 struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1];
668 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
669 u32 node, depth, type, lowbound, upbound;
670 static const char * const scope_str[] = {"", " zone", " cluster",
671 " node"};
672
673 nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX,
674 attrs[TIPC_NLA_NAME_TABLE], NULL);
675
676 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, nt[TIPC_NLA_NAME_TABLE_PUBL],
677 NULL);
678
679 ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req);
680
681 depth = ntohl(ntq->depth);
682 type = ntohl(ntq->type);
683 lowbound = ntohl(ntq->lowbound);
684 upbound = ntohl(ntq->upbound);
685
686 if (!(depth & TIPC_NTQ_ALLTYPES) &&
687 (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])))
688 return 0;
689 if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])))
690 return 0;
691 if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER])))
692 return 0;
693
694 tipc_tlv_sprintf(msg->rep, "%-10u ",
695 nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
696
697 if (depth == 1)
698 goto out;
699
700 tipc_tlv_sprintf(msg->rep, "%-10u %-10u ",
701 nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
702 nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
703
704 if (depth == 2)
705 goto out;
706
707 node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]);
708 sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node),
709 tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF]));
710 tipc_tlv_sprintf(msg->rep, "%-26s ", port_str);
711
712 if (depth == 3)
713 goto out;
714
715 tipc_tlv_sprintf(msg->rep, "%-10u %s",
716 nla_get_u32(publ[TIPC_NLA_PUBL_REF]),
717 scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
718out:
719 tipc_tlv_sprintf(msg->rep, "\n");
720
721 return 0;
722}
723
Richard Alpe487d2a32015-02-09 09:50:11 +0100724static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg,
725 struct nlattr **attrs)
726{
727 u32 type, lower, upper;
728 struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1];
729
730 nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL], NULL);
731
732 type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]);
733 lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]);
734 upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]);
735
736 if (lower == upper)
737 tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower);
738 else
739 tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper);
740
741 return 0;
742}
743
744static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock)
745{
746 int err;
747 void *hdr;
748 struct nlattr *nest;
749 struct sk_buff *args;
750 struct tipc_nl_compat_cmd_dump dump;
751
752 args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
753 if (!args)
754 return -ENOMEM;
755
756 hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI,
757 TIPC_NL_PUBL_GET);
758
759 nest = nla_nest_start(args, TIPC_NLA_SOCK);
760 if (!nest) {
761 kfree_skb(args);
762 return -EMSGSIZE;
763 }
764
765 if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) {
766 kfree_skb(args);
767 return -EMSGSIZE;
768 }
769
770 nla_nest_end(args, nest);
771 genlmsg_end(args, hdr);
772
773 dump.dumpit = tipc_nl_publ_dump;
774 dump.format = __tipc_nl_compat_publ_dump;
775
776 err = __tipc_nl_compat_dumpit(&dump, msg, args);
777
778 kfree_skb(args);
779
780 return err;
781}
782
783static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg,
784 struct nlattr **attrs)
785{
786 int err;
787 u32 sock_ref;
788 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
789
790 nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK], NULL);
791
792 sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
793 tipc_tlv_sprintf(msg->rep, "%u:", sock_ref);
794
795 if (sock[TIPC_NLA_SOCK_CON]) {
796 u32 node;
797 struct nlattr *con[TIPC_NLA_CON_MAX + 1];
798
799 nla_parse_nested(con, TIPC_NLA_CON_MAX, sock[TIPC_NLA_SOCK_CON],
800 NULL);
801
802 node = nla_get_u32(con[TIPC_NLA_CON_NODE]);
803 tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>",
804 tipc_zone(node),
805 tipc_cluster(node),
806 tipc_node(node),
807 nla_get_u32(con[TIPC_NLA_CON_SOCK]));
808
809 if (con[TIPC_NLA_CON_FLAG])
810 tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n",
811 nla_get_u32(con[TIPC_NLA_CON_TYPE]),
812 nla_get_u32(con[TIPC_NLA_CON_INST]));
813 else
814 tipc_tlv_sprintf(msg->rep, "\n");
815 } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) {
816 tipc_tlv_sprintf(msg->rep, " bound to");
817
818 err = tipc_nl_compat_publ_dump(msg, sock_ref);
819 if (err)
820 return err;
821 }
822 tipc_tlv_sprintf(msg->rep, "\n");
823
824 return 0;
825}
826
Richard Alpe5bfc3352015-02-09 09:50:12 +0100827static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg,
828 struct nlattr **attrs)
829{
830 struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1];
831
832 nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, attrs[TIPC_NLA_MEDIA],
833 NULL);
834
835 return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME,
836 nla_data(media[TIPC_NLA_MEDIA_NAME]),
837 nla_len(media[TIPC_NLA_MEDIA_NAME]));
838}
839
Richard Alpe4b28cb52015-02-09 09:50:13 +0100840static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg,
841 struct nlattr **attrs)
842{
843 struct tipc_node_info node_info;
844 struct nlattr *node[TIPC_NLA_NODE_MAX + 1];
845
846 nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE], NULL);
847
848 node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR]));
849 node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP]));
850
851 return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info,
852 sizeof(node_info));
853}
854
Richard Alped7cc75d2015-02-09 09:50:14 +0100855static int tipc_nl_compat_net_set(struct sk_buff *skb,
856 struct tipc_nl_compat_msg *msg)
857{
858 u32 val;
859 struct nlattr *net;
860
861 val = ntohl(*(__be32 *)TLV_DATA(msg->req));
862
863 net = nla_nest_start(skb, TIPC_NLA_NET);
864 if (!net)
865 return -EMSGSIZE;
866
Richard Alpe964f9502015-02-09 09:50:15 +0100867 if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) {
868 if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val))
869 return -EMSGSIZE;
870 } else if (msg->cmd == TIPC_CMD_SET_NETID) {
871 if (nla_put_u32(skb, TIPC_NLA_NET_ID, val))
872 return -EMSGSIZE;
873 }
Richard Alped7cc75d2015-02-09 09:50:14 +0100874 nla_nest_end(skb, net);
875
876 return 0;
877}
878
Richard Alpe3c261812015-02-09 09:50:16 +0100879static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg,
880 struct nlattr **attrs)
881{
882 __be32 id;
883 struct nlattr *net[TIPC_NLA_NET_MAX + 1];
884
885 nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET], NULL);
886 id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID]));
887
888 return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id));
889}
890
Richard Alpe5a81a632015-02-09 09:50:17 +0100891static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg)
892{
893 msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN);
894 if (!msg->rep)
895 return -ENOMEM;
896
897 tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING);
898 tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n");
899
900 return 0;
901}
902
Richard Alped0796d12015-02-09 09:50:04 +0100903static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg)
904{
905 struct tipc_nl_compat_cmd_dump dump;
Richard Alpe9ab15462015-02-09 09:50:05 +0100906 struct tipc_nl_compat_cmd_doit doit;
Richard Alped0796d12015-02-09 09:50:04 +0100907
908 memset(&dump, 0, sizeof(dump));
Richard Alpe9ab15462015-02-09 09:50:05 +0100909 memset(&doit, 0, sizeof(doit));
Richard Alped0796d12015-02-09 09:50:04 +0100910
911 switch (msg->cmd) {
912 case TIPC_CMD_GET_BEARER_NAMES:
913 msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME);
914 dump.dumpit = tipc_nl_bearer_dump;
915 dump.format = tipc_nl_compat_bearer_dump;
916 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe9ab15462015-02-09 09:50:05 +0100917 case TIPC_CMD_ENABLE_BEARER:
918 msg->req_type = TIPC_TLV_BEARER_CONFIG;
919 doit.doit = tipc_nl_bearer_enable;
920 doit.transcode = tipc_nl_compat_bearer_enable;
921 return tipc_nl_compat_doit(&doit, msg);
922 case TIPC_CMD_DISABLE_BEARER:
923 msg->req_type = TIPC_TLV_BEARER_NAME;
924 doit.doit = tipc_nl_bearer_disable;
925 doit.transcode = tipc_nl_compat_bearer_disable;
926 return tipc_nl_compat_doit(&doit, msg);
Richard Alpef2b3b2d2015-02-09 09:50:06 +0100927 case TIPC_CMD_SHOW_LINK_STATS:
928 msg->req_type = TIPC_TLV_LINK_NAME;
929 msg->rep_size = ULTRA_STRING_MAX_LEN;
930 msg->rep_type = TIPC_TLV_ULTRA_STRING;
931 dump.dumpit = tipc_nl_link_dump;
932 dump.format = tipc_nl_compat_link_stat_dump;
933 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe357ebdb2015-02-09 09:50:07 +0100934 case TIPC_CMD_GET_LINKS:
935 msg->req_type = TIPC_TLV_NET_ADDR;
936 msg->rep_size = ULTRA_STRING_MAX_LEN;
937 dump.dumpit = tipc_nl_link_dump;
938 dump.format = tipc_nl_compat_link_dump;
939 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe37e2d482015-02-09 09:50:08 +0100940 case TIPC_CMD_SET_LINK_TOL:
941 case TIPC_CMD_SET_LINK_PRI:
942 case TIPC_CMD_SET_LINK_WINDOW:
943 msg->req_type = TIPC_TLV_LINK_CONFIG;
944 doit.doit = tipc_nl_link_set;
945 doit.transcode = tipc_nl_compat_link_set;
946 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe18178772015-02-09 09:50:09 +0100947 case TIPC_CMD_RESET_LINK_STATS:
948 msg->req_type = TIPC_TLV_LINK_NAME;
949 doit.doit = tipc_nl_link_reset_stats;
950 doit.transcode = tipc_nl_compat_link_reset_stats;
951 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe44a8ae92015-02-09 09:50:10 +0100952 case TIPC_CMD_SHOW_NAME_TABLE:
953 msg->req_type = TIPC_TLV_NAME_TBL_QUERY;
954 msg->rep_size = ULTRA_STRING_MAX_LEN;
955 msg->rep_type = TIPC_TLV_ULTRA_STRING;
956 dump.header = tipc_nl_compat_name_table_dump_header;
957 dump.dumpit = tipc_nl_name_table_dump;
958 dump.format = tipc_nl_compat_name_table_dump;
959 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe487d2a32015-02-09 09:50:11 +0100960 case TIPC_CMD_SHOW_PORTS:
961 msg->rep_size = ULTRA_STRING_MAX_LEN;
962 msg->rep_type = TIPC_TLV_ULTRA_STRING;
963 dump.dumpit = tipc_nl_sk_dump;
964 dump.format = tipc_nl_compat_sk_dump;
965 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5bfc3352015-02-09 09:50:12 +0100966 case TIPC_CMD_GET_MEDIA_NAMES:
967 msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME);
968 dump.dumpit = tipc_nl_media_dump;
969 dump.format = tipc_nl_compat_media_dump;
970 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe4b28cb52015-02-09 09:50:13 +0100971 case TIPC_CMD_GET_NODES:
972 msg->rep_size = ULTRA_STRING_MAX_LEN;
973 dump.dumpit = tipc_nl_node_dump;
974 dump.format = tipc_nl_compat_node_dump;
975 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alped7cc75d2015-02-09 09:50:14 +0100976 case TIPC_CMD_SET_NODE_ADDR:
977 msg->req_type = TIPC_TLV_NET_ADDR;
978 doit.doit = tipc_nl_net_set;
979 doit.transcode = tipc_nl_compat_net_set;
980 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe964f9502015-02-09 09:50:15 +0100981 case TIPC_CMD_SET_NETID:
982 msg->req_type = TIPC_TLV_UNSIGNED;
983 doit.doit = tipc_nl_net_set;
984 doit.transcode = tipc_nl_compat_net_set;
985 return tipc_nl_compat_doit(&doit, msg);
Richard Alpe3c261812015-02-09 09:50:16 +0100986 case TIPC_CMD_GET_NETID:
987 msg->rep_size = sizeof(u32);
988 dump.dumpit = tipc_nl_net_dump;
989 dump.format = tipc_nl_compat_net_dump;
990 return tipc_nl_compat_dumpit(&dump, msg);
Richard Alpe5a81a632015-02-09 09:50:17 +0100991 case TIPC_CMD_SHOW_STATS:
992 return tipc_cmd_show_stats_compat(msg);
Richard Alped0796d12015-02-09 09:50:04 +0100993 }
994
995 return -EOPNOTSUPP;
996}
997
998static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info)
999{
1000 int err;
1001 int len;
1002 struct tipc_nl_compat_msg msg;
1003 struct nlmsghdr *req_nlh;
1004 struct nlmsghdr *rep_nlh;
1005 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1006 struct net *net = genl_info_net(info);
1007
1008 memset(&msg, 0, sizeof(msg));
1009
1010 req_nlh = (struct nlmsghdr *)skb->data;
1011 msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN;
1012 msg.cmd = req_userhdr->cmd;
1013 msg.dst_sk = info->dst_sk;
1014
1015 if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) {
1016 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN);
1017 err = -EACCES;
1018 goto send;
1019 }
1020
1021 len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN);
1022 if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) {
1023 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1024 err = -EOPNOTSUPP;
1025 goto send;
1026 }
1027
1028 err = tipc_nl_compat_handle(&msg);
1029 if (err == -EOPNOTSUPP)
1030 msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED);
1031 else if (err == -EINVAL)
1032 msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR);
1033send:
1034 if (!msg.rep)
1035 return err;
1036
1037 len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1038 skb_push(msg.rep, len);
1039 rep_nlh = nlmsg_hdr(msg.rep);
1040 memcpy(rep_nlh, info->nlhdr, len);
1041 rep_nlh->nlmsg_len = msg.rep->len;
1042 genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid);
1043
1044 return err;
1045}
1046
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001047static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
1048{
1049 struct net *net = genl_info_net(info);
1050 struct sk_buff *rep_buf;
1051 struct nlmsghdr *rep_nlh;
1052 struct nlmsghdr *req_nlh = info->nlhdr;
1053 struct tipc_genlmsghdr *req_userhdr = info->userhdr;
1054 int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN);
1055 u16 cmd;
1056
1057 if ((req_userhdr->cmd & 0xC000) &&
1058 (!netlink_net_capable(skb, CAP_NET_ADMIN)))
1059 cmd = TIPC_CMD_NOT_NET_ADMIN;
1060 else
1061 cmd = req_userhdr->cmd;
1062
1063 rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd,
1064 nlmsg_data(req_nlh) + GENL_HDRLEN +
1065 TIPC_GENL_HDRLEN,
1066 nlmsg_attrlen(req_nlh, GENL_HDRLEN +
1067 TIPC_GENL_HDRLEN), hdr_space);
1068
1069 if (rep_buf) {
1070 skb_push(rep_buf, hdr_space);
1071 rep_nlh = nlmsg_hdr(rep_buf);
1072 memcpy(rep_nlh, req_nlh, hdr_space);
1073 rep_nlh->nlmsg_len = rep_buf->len;
1074 genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid);
1075 }
1076
1077 return 0;
1078}
1079
Richard Alped0796d12015-02-09 09:50:04 +01001080/* Temporary function to keep functionality throughout the patchset
1081 * without having to mess with the global variables and other trickery
1082 * of the old API.
1083 */
1084static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info)
1085{
1086 struct tipc_genlmsghdr *req = info->userhdr;
1087
1088 switch (req->cmd) {
1089 case TIPC_CMD_GET_BEARER_NAMES:
Richard Alpe9ab15462015-02-09 09:50:05 +01001090 case TIPC_CMD_ENABLE_BEARER:
1091 case TIPC_CMD_DISABLE_BEARER:
Richard Alpef2b3b2d2015-02-09 09:50:06 +01001092 case TIPC_CMD_SHOW_LINK_STATS:
Richard Alpe357ebdb2015-02-09 09:50:07 +01001093 case TIPC_CMD_GET_LINKS:
Richard Alpe37e2d482015-02-09 09:50:08 +01001094 case TIPC_CMD_SET_LINK_TOL:
1095 case TIPC_CMD_SET_LINK_PRI:
1096 case TIPC_CMD_SET_LINK_WINDOW:
Richard Alpe18178772015-02-09 09:50:09 +01001097 case TIPC_CMD_RESET_LINK_STATS:
Richard Alpe44a8ae92015-02-09 09:50:10 +01001098 case TIPC_CMD_SHOW_NAME_TABLE:
Richard Alpe487d2a32015-02-09 09:50:11 +01001099 case TIPC_CMD_SHOW_PORTS:
Richard Alpe5bfc3352015-02-09 09:50:12 +01001100 case TIPC_CMD_GET_MEDIA_NAMES:
Richard Alpe4b28cb52015-02-09 09:50:13 +01001101 case TIPC_CMD_GET_NODES:
Richard Alped7cc75d2015-02-09 09:50:14 +01001102 case TIPC_CMD_SET_NODE_ADDR:
Richard Alpe964f9502015-02-09 09:50:15 +01001103 case TIPC_CMD_SET_NETID:
Richard Alpe3c261812015-02-09 09:50:16 +01001104 case TIPC_CMD_GET_NETID:
Richard Alpe5a81a632015-02-09 09:50:17 +01001105 case TIPC_CMD_SHOW_STATS:
Richard Alped0796d12015-02-09 09:50:04 +01001106 return tipc_nl_compat_recv(skb, info);
1107 }
1108
1109 return handle_cmd(skb, info);
1110}
1111
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001112static struct genl_family tipc_genl_compat_family = {
1113 .id = GENL_ID_GENERATE,
1114 .name = TIPC_GENL_NAME,
1115 .version = TIPC_GENL_VERSION,
1116 .hdrsize = TIPC_GENL_HDRLEN,
1117 .maxattr = 0,
1118 .netnsok = true,
1119};
1120
1121static struct genl_ops tipc_genl_compat_ops[] = {
1122 {
1123 .cmd = TIPC_GENL_CMD,
Richard Alped0796d12015-02-09 09:50:04 +01001124 .doit = tipc_nl_compat_tmp_wrap,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01001125 },
1126};
1127
1128int tipc_netlink_compat_start(void)
1129{
1130 int res;
1131
1132 res = genl_register_family_with_ops(&tipc_genl_compat_family,
1133 tipc_genl_compat_ops);
1134 if (res) {
1135 pr_err("Failed to register legacy compat interface\n");
1136 return res;
1137 }
1138
1139 return 0;
1140}
1141
1142void tipc_netlink_compat_stop(void)
1143{
1144 genl_unregister_family(&tipc_genl_compat_family);
1145}