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