Commit | Line | Data |
---|---|---|
bfb3e5dd RA |
1 | /* |
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" | |
d0796d1e | 36 | #include "bearer.h" |
f2b3b2d4 | 37 | #include "link.h" |
44a8ae94 | 38 | #include "name_table.h" |
487d2a3a | 39 | #include "socket.h" |
4b28cb58 | 40 | #include "node.h" |
964f9501 | 41 | #include "net.h" |
bfb3e5dd RA |
42 | #include <net/genetlink.h> |
43 | #include <linux/tipc_config.h> | |
44 | ||
d0796d1e RA |
45 | /* 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 | ||
54 | struct tipc_nl_compat_msg { | |
55 | u16 cmd; | |
f2b3b2d4 | 56 | int rep_type; |
d0796d1e | 57 | int rep_size; |
9ab15465 | 58 | int req_type; |
d0796d1e RA |
59 | struct sk_buff *rep; |
60 | struct tlv_desc *req; | |
61 | struct sock *dst_sk; | |
62 | }; | |
63 | ||
64 | struct tipc_nl_compat_cmd_dump { | |
44a8ae94 | 65 | int (*header)(struct tipc_nl_compat_msg *); |
d0796d1e RA |
66 | int (*dumpit)(struct sk_buff *, struct netlink_callback *); |
67 | int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs); | |
68 | }; | |
69 | ||
9ab15465 RA |
70 | struct 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 | ||
d0796d1e RA |
75 | static 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 | ||
89 | static 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 | ||
f2b3b2d4 RA |
105 | static 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 | ||
114 | static 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 | ||
d0796d1e RA |
139 | static 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 | ||
156 | static 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 | ||
168 | static 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 | ||
217 | err_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 | ||
238 | static 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 | ||
f2b3b2d4 RA |
244 | if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type)) |
245 | return -EINVAL; | |
246 | ||
d0796d1e RA |
247 | msg->rep = tipc_tlv_alloc(msg->rep_size); |
248 | if (!msg->rep) | |
249 | return -ENOMEM; | |
250 | ||
f2b3b2d4 RA |
251 | if (msg->rep_type) |
252 | tipc_tlv_init(msg->rep, msg->rep_type); | |
253 | ||
44a8ae94 RA |
254 | if (cmd->header) |
255 | (*cmd->header)(msg); | |
256 | ||
d0796d1e RA |
257 | 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 | ||
9ab15465 RA |
272 | static 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); | |
316 | parse_out: | |
317 | kfree(attrbuf); | |
318 | trans_out: | |
319 | kfree_skb(trans_buf); | |
320 | ||
321 | return err; | |
322 | } | |
323 | ||
324 | static 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 | ||
d0796d1e RA |
344 | static 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 | ||
9ab15465 RA |
357 | static 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 | ||
389 | static 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 | ||
f2b3b2d4 RA |
409 | static inline u32 perc(u32 count, u32 total) |
410 | { | |
411 | return (count * 100 + (total / 2)) / total; | |
412 | } | |
413 | ||
414 | static 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 | ||
453 | static 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 | ||
357ebdbf RA |
564 | static 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 | ||
37e2d484 RA |
580 | static 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 | ||
1817877b RA |
617 | static 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 | ||
44a8ae94 RA |
637 | static 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 | ||
662 | static 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])]); | |
718 | out: | |
719 | tipc_tlv_sprintf(msg->rep, "\n"); | |
720 | ||
721 | return 0; | |
722 | } | |
723 | ||
487d2a3a RA |
724 | static 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 | ||
744 | static 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 | ||
783 | static 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 | ||
5bfc335a RA |
827 | static 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 | ||
4b28cb58 RA |
840 | static 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 | ||
d7cc75d3 RA |
855 | static 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 | ||
964f9501 RA |
867 | 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 | } | |
d7cc75d3 RA |
874 | nla_nest_end(skb, net); |
875 | ||
876 | return 0; | |
877 | } | |
878 | ||
3c26181c RA |
879 | static 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 | ||
d0796d1e RA |
891 | static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg) |
892 | { | |
893 | struct tipc_nl_compat_cmd_dump dump; | |
9ab15465 | 894 | struct tipc_nl_compat_cmd_doit doit; |
d0796d1e RA |
895 | |
896 | memset(&dump, 0, sizeof(dump)); | |
9ab15465 | 897 | memset(&doit, 0, sizeof(doit)); |
d0796d1e RA |
898 | |
899 | switch (msg->cmd) { | |
900 | case TIPC_CMD_GET_BEARER_NAMES: | |
901 | msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME); | |
902 | dump.dumpit = tipc_nl_bearer_dump; | |
903 | dump.format = tipc_nl_compat_bearer_dump; | |
904 | return tipc_nl_compat_dumpit(&dump, msg); | |
9ab15465 RA |
905 | case TIPC_CMD_ENABLE_BEARER: |
906 | msg->req_type = TIPC_TLV_BEARER_CONFIG; | |
907 | doit.doit = tipc_nl_bearer_enable; | |
908 | doit.transcode = tipc_nl_compat_bearer_enable; | |
909 | return tipc_nl_compat_doit(&doit, msg); | |
910 | case TIPC_CMD_DISABLE_BEARER: | |
911 | msg->req_type = TIPC_TLV_BEARER_NAME; | |
912 | doit.doit = tipc_nl_bearer_disable; | |
913 | doit.transcode = tipc_nl_compat_bearer_disable; | |
914 | return tipc_nl_compat_doit(&doit, msg); | |
f2b3b2d4 RA |
915 | case TIPC_CMD_SHOW_LINK_STATS: |
916 | msg->req_type = TIPC_TLV_LINK_NAME; | |
917 | msg->rep_size = ULTRA_STRING_MAX_LEN; | |
918 | msg->rep_type = TIPC_TLV_ULTRA_STRING; | |
919 | dump.dumpit = tipc_nl_link_dump; | |
920 | dump.format = tipc_nl_compat_link_stat_dump; | |
921 | return tipc_nl_compat_dumpit(&dump, msg); | |
357ebdbf RA |
922 | case TIPC_CMD_GET_LINKS: |
923 | msg->req_type = TIPC_TLV_NET_ADDR; | |
924 | msg->rep_size = ULTRA_STRING_MAX_LEN; | |
925 | dump.dumpit = tipc_nl_link_dump; | |
926 | dump.format = tipc_nl_compat_link_dump; | |
927 | return tipc_nl_compat_dumpit(&dump, msg); | |
37e2d484 RA |
928 | case TIPC_CMD_SET_LINK_TOL: |
929 | case TIPC_CMD_SET_LINK_PRI: | |
930 | case TIPC_CMD_SET_LINK_WINDOW: | |
931 | msg->req_type = TIPC_TLV_LINK_CONFIG; | |
932 | doit.doit = tipc_nl_link_set; | |
933 | doit.transcode = tipc_nl_compat_link_set; | |
934 | return tipc_nl_compat_doit(&doit, msg); | |
1817877b RA |
935 | case TIPC_CMD_RESET_LINK_STATS: |
936 | msg->req_type = TIPC_TLV_LINK_NAME; | |
937 | doit.doit = tipc_nl_link_reset_stats; | |
938 | doit.transcode = tipc_nl_compat_link_reset_stats; | |
939 | return tipc_nl_compat_doit(&doit, msg); | |
44a8ae94 RA |
940 | case TIPC_CMD_SHOW_NAME_TABLE: |
941 | msg->req_type = TIPC_TLV_NAME_TBL_QUERY; | |
942 | msg->rep_size = ULTRA_STRING_MAX_LEN; | |
943 | msg->rep_type = TIPC_TLV_ULTRA_STRING; | |
944 | dump.header = tipc_nl_compat_name_table_dump_header; | |
945 | dump.dumpit = tipc_nl_name_table_dump; | |
946 | dump.format = tipc_nl_compat_name_table_dump; | |
947 | return tipc_nl_compat_dumpit(&dump, msg); | |
487d2a3a RA |
948 | case TIPC_CMD_SHOW_PORTS: |
949 | msg->rep_size = ULTRA_STRING_MAX_LEN; | |
950 | msg->rep_type = TIPC_TLV_ULTRA_STRING; | |
951 | dump.dumpit = tipc_nl_sk_dump; | |
952 | dump.format = tipc_nl_compat_sk_dump; | |
953 | return tipc_nl_compat_dumpit(&dump, msg); | |
5bfc335a RA |
954 | case TIPC_CMD_GET_MEDIA_NAMES: |
955 | msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME); | |
956 | dump.dumpit = tipc_nl_media_dump; | |
957 | dump.format = tipc_nl_compat_media_dump; | |
958 | return tipc_nl_compat_dumpit(&dump, msg); | |
4b28cb58 RA |
959 | case TIPC_CMD_GET_NODES: |
960 | msg->rep_size = ULTRA_STRING_MAX_LEN; | |
961 | dump.dumpit = tipc_nl_node_dump; | |
962 | dump.format = tipc_nl_compat_node_dump; | |
963 | return tipc_nl_compat_dumpit(&dump, msg); | |
d7cc75d3 RA |
964 | case TIPC_CMD_SET_NODE_ADDR: |
965 | msg->req_type = TIPC_TLV_NET_ADDR; | |
966 | doit.doit = tipc_nl_net_set; | |
967 | doit.transcode = tipc_nl_compat_net_set; | |
968 | return tipc_nl_compat_doit(&doit, msg); | |
964f9501 RA |
969 | case TIPC_CMD_SET_NETID: |
970 | msg->req_type = TIPC_TLV_UNSIGNED; | |
971 | doit.doit = tipc_nl_net_set; | |
972 | doit.transcode = tipc_nl_compat_net_set; | |
973 | return tipc_nl_compat_doit(&doit, msg); | |
3c26181c RA |
974 | case TIPC_CMD_GET_NETID: |
975 | msg->rep_size = sizeof(u32); | |
976 | dump.dumpit = tipc_nl_net_dump; | |
977 | dump.format = tipc_nl_compat_net_dump; | |
978 | return tipc_nl_compat_dumpit(&dump, msg); | |
d0796d1e RA |
979 | } |
980 | ||
981 | return -EOPNOTSUPP; | |
982 | } | |
983 | ||
984 | static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info) | |
985 | { | |
986 | int err; | |
987 | int len; | |
988 | struct tipc_nl_compat_msg msg; | |
989 | struct nlmsghdr *req_nlh; | |
990 | struct nlmsghdr *rep_nlh; | |
991 | struct tipc_genlmsghdr *req_userhdr = info->userhdr; | |
992 | struct net *net = genl_info_net(info); | |
993 | ||
994 | memset(&msg, 0, sizeof(msg)); | |
995 | ||
996 | req_nlh = (struct nlmsghdr *)skb->data; | |
997 | msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN; | |
998 | msg.cmd = req_userhdr->cmd; | |
999 | msg.dst_sk = info->dst_sk; | |
1000 | ||
1001 | if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) { | |
1002 | msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN); | |
1003 | err = -EACCES; | |
1004 | goto send; | |
1005 | } | |
1006 | ||
1007 | len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN); | |
1008 | if (TLV_GET_LEN(msg.req) && !TLV_OK(msg.req, len)) { | |
1009 | msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED); | |
1010 | err = -EOPNOTSUPP; | |
1011 | goto send; | |
1012 | } | |
1013 | ||
1014 | err = tipc_nl_compat_handle(&msg); | |
1015 | if (err == -EOPNOTSUPP) | |
1016 | msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED); | |
1017 | else if (err == -EINVAL) | |
1018 | msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR); | |
1019 | send: | |
1020 | if (!msg.rep) | |
1021 | return err; | |
1022 | ||
1023 | len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN); | |
1024 | skb_push(msg.rep, len); | |
1025 | rep_nlh = nlmsg_hdr(msg.rep); | |
1026 | memcpy(rep_nlh, info->nlhdr, len); | |
1027 | rep_nlh->nlmsg_len = msg.rep->len; | |
1028 | genlmsg_unicast(net, msg.rep, NETLINK_CB(skb).portid); | |
1029 | ||
1030 | return err; | |
1031 | } | |
1032 | ||
bfb3e5dd RA |
1033 | static int handle_cmd(struct sk_buff *skb, struct genl_info *info) |
1034 | { | |
1035 | struct net *net = genl_info_net(info); | |
1036 | struct sk_buff *rep_buf; | |
1037 | struct nlmsghdr *rep_nlh; | |
1038 | struct nlmsghdr *req_nlh = info->nlhdr; | |
1039 | struct tipc_genlmsghdr *req_userhdr = info->userhdr; | |
1040 | int hdr_space = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN); | |
1041 | u16 cmd; | |
1042 | ||
1043 | if ((req_userhdr->cmd & 0xC000) && | |
1044 | (!netlink_net_capable(skb, CAP_NET_ADMIN))) | |
1045 | cmd = TIPC_CMD_NOT_NET_ADMIN; | |
1046 | else | |
1047 | cmd = req_userhdr->cmd; | |
1048 | ||
1049 | rep_buf = tipc_cfg_do_cmd(net, req_userhdr->dest, cmd, | |
1050 | nlmsg_data(req_nlh) + GENL_HDRLEN + | |
1051 | TIPC_GENL_HDRLEN, | |
1052 | nlmsg_attrlen(req_nlh, GENL_HDRLEN + | |
1053 | TIPC_GENL_HDRLEN), hdr_space); | |
1054 | ||
1055 | if (rep_buf) { | |
1056 | skb_push(rep_buf, hdr_space); | |
1057 | rep_nlh = nlmsg_hdr(rep_buf); | |
1058 | memcpy(rep_nlh, req_nlh, hdr_space); | |
1059 | rep_nlh->nlmsg_len = rep_buf->len; | |
1060 | genlmsg_unicast(net, rep_buf, NETLINK_CB(skb).portid); | |
1061 | } | |
1062 | ||
1063 | return 0; | |
1064 | } | |
1065 | ||
d0796d1e RA |
1066 | /* Temporary function to keep functionality throughout the patchset |
1067 | * without having to mess with the global variables and other trickery | |
1068 | * of the old API. | |
1069 | */ | |
1070 | static int tipc_nl_compat_tmp_wrap(struct sk_buff *skb, struct genl_info *info) | |
1071 | { | |
1072 | struct tipc_genlmsghdr *req = info->userhdr; | |
1073 | ||
1074 | switch (req->cmd) { | |
1075 | case TIPC_CMD_GET_BEARER_NAMES: | |
9ab15465 RA |
1076 | case TIPC_CMD_ENABLE_BEARER: |
1077 | case TIPC_CMD_DISABLE_BEARER: | |
f2b3b2d4 | 1078 | case TIPC_CMD_SHOW_LINK_STATS: |
357ebdbf | 1079 | case TIPC_CMD_GET_LINKS: |
37e2d484 RA |
1080 | case TIPC_CMD_SET_LINK_TOL: |
1081 | case TIPC_CMD_SET_LINK_PRI: | |
1082 | case TIPC_CMD_SET_LINK_WINDOW: | |
1817877b | 1083 | case TIPC_CMD_RESET_LINK_STATS: |
44a8ae94 | 1084 | case TIPC_CMD_SHOW_NAME_TABLE: |
487d2a3a | 1085 | case TIPC_CMD_SHOW_PORTS: |
5bfc335a | 1086 | case TIPC_CMD_GET_MEDIA_NAMES: |
4b28cb58 | 1087 | case TIPC_CMD_GET_NODES: |
d7cc75d3 | 1088 | case TIPC_CMD_SET_NODE_ADDR: |
964f9501 | 1089 | case TIPC_CMD_SET_NETID: |
3c26181c | 1090 | case TIPC_CMD_GET_NETID: |
d0796d1e RA |
1091 | return tipc_nl_compat_recv(skb, info); |
1092 | } | |
1093 | ||
1094 | return handle_cmd(skb, info); | |
1095 | } | |
1096 | ||
bfb3e5dd RA |
1097 | static struct genl_family tipc_genl_compat_family = { |
1098 | .id = GENL_ID_GENERATE, | |
1099 | .name = TIPC_GENL_NAME, | |
1100 | .version = TIPC_GENL_VERSION, | |
1101 | .hdrsize = TIPC_GENL_HDRLEN, | |
1102 | .maxattr = 0, | |
1103 | .netnsok = true, | |
1104 | }; | |
1105 | ||
1106 | static struct genl_ops tipc_genl_compat_ops[] = { | |
1107 | { | |
1108 | .cmd = TIPC_GENL_CMD, | |
d0796d1e | 1109 | .doit = tipc_nl_compat_tmp_wrap, |
bfb3e5dd RA |
1110 | }, |
1111 | }; | |
1112 | ||
1113 | int tipc_netlink_compat_start(void) | |
1114 | { | |
1115 | int res; | |
1116 | ||
1117 | res = genl_register_family_with_ops(&tipc_genl_compat_family, | |
1118 | tipc_genl_compat_ops); | |
1119 | if (res) { | |
1120 | pr_err("Failed to register legacy compat interface\n"); | |
1121 | return res; | |
1122 | } | |
1123 | ||
1124 | return 0; | |
1125 | } | |
1126 | ||
1127 | void tipc_netlink_compat_stop(void) | |
1128 | { | |
1129 | genl_unregister_family(&tipc_genl_compat_family); | |
1130 | } |