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" |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 35 | #include "bearer.h" |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 36 | #include "link.h" |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 37 | #include "name_table.h" |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 38 | #include "socket.h" |
Richard Alpe | 4b28cb5 | 2015-02-09 09:50:13 +0100 | [diff] [blame] | 39 | #include "node.h" |
Richard Alpe | 964f950 | 2015-02-09 09:50:15 +0100 | [diff] [blame] | 40 | #include "net.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 | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 58 | struct net *net; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 59 | struct sk_buff *rep; |
| 60 | struct tlv_desc *req; |
| 61 | struct sock *dst_sk; |
| 62 | }; |
| 63 | |
| 64 | struct tipc_nl_compat_cmd_dump { |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 65 | int (*header)(struct tipc_nl_compat_msg *); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 66 | int (*dumpit)(struct sk_buff *, struct netlink_callback *); |
| 67 | int (*format)(struct tipc_nl_compat_msg *msg, struct nlattr **attrs); |
| 68 | }; |
| 69 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 70 | struct tipc_nl_compat_cmd_doit { |
| 71 | int (*doit)(struct sk_buff *skb, struct genl_info *info); |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 72 | int (*transcode)(struct tipc_nl_compat_cmd_doit *cmd, |
| 73 | struct sk_buff *skb, struct tipc_nl_compat_msg *msg); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 76 | static int tipc_skb_tailroom(struct sk_buff *skb) |
| 77 | { |
| 78 | int tailroom; |
| 79 | int limit; |
| 80 | |
| 81 | tailroom = skb_tailroom(skb); |
| 82 | limit = TIPC_SKB_MAX - skb->len; |
| 83 | |
| 84 | if (tailroom < limit) |
| 85 | return tailroom; |
| 86 | |
| 87 | return limit; |
| 88 | } |
| 89 | |
Ying Xue | 8b66fee | 2019-01-14 17:22:25 +0800 | [diff] [blame] | 90 | static inline int TLV_GET_DATA_LEN(struct tlv_desc *tlv) |
| 91 | { |
| 92 | return TLV_GET_LEN(tlv) - TLV_SPACE(0); |
| 93 | } |
| 94 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 95 | static int tipc_add_tlv(struct sk_buff *skb, u16 type, void *data, u16 len) |
| 96 | { |
| 97 | struct tlv_desc *tlv = (struct tlv_desc *)skb_tail_pointer(skb); |
| 98 | |
| 99 | if (tipc_skb_tailroom(skb) < TLV_SPACE(len)) |
| 100 | return -EMSGSIZE; |
| 101 | |
| 102 | skb_put(skb, TLV_SPACE(len)); |
| 103 | tlv->tlv_type = htons(type); |
| 104 | tlv->tlv_len = htons(TLV_LENGTH(len)); |
| 105 | if (len && data) |
| 106 | memcpy(TLV_DATA(tlv), data, len); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 111 | static void tipc_tlv_init(struct sk_buff *skb, u16 type) |
| 112 | { |
| 113 | struct tlv_desc *tlv = (struct tlv_desc *)skb->data; |
| 114 | |
| 115 | TLV_SET_LEN(tlv, 0); |
| 116 | TLV_SET_TYPE(tlv, type); |
| 117 | skb_put(skb, sizeof(struct tlv_desc)); |
| 118 | } |
| 119 | |
| 120 | static int tipc_tlv_sprintf(struct sk_buff *skb, const char *fmt, ...) |
| 121 | { |
| 122 | int n; |
| 123 | u16 len; |
| 124 | u32 rem; |
| 125 | char *buf; |
| 126 | struct tlv_desc *tlv; |
| 127 | va_list args; |
| 128 | |
| 129 | rem = tipc_skb_tailroom(skb); |
| 130 | |
| 131 | tlv = (struct tlv_desc *)skb->data; |
| 132 | len = TLV_GET_LEN(tlv); |
| 133 | buf = TLV_DATA(tlv) + len; |
| 134 | |
| 135 | va_start(args, fmt); |
| 136 | n = vscnprintf(buf, rem, fmt, args); |
| 137 | va_end(args); |
| 138 | |
| 139 | TLV_SET_LEN(tlv, n + len); |
| 140 | skb_put(skb, n); |
| 141 | |
| 142 | return n; |
| 143 | } |
| 144 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 145 | static struct sk_buff *tipc_tlv_alloc(int size) |
| 146 | { |
| 147 | int hdr_len; |
| 148 | struct sk_buff *buf; |
| 149 | |
| 150 | size = TLV_SPACE(size); |
| 151 | hdr_len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN); |
| 152 | |
| 153 | buf = alloc_skb(hdr_len + size, GFP_KERNEL); |
| 154 | if (!buf) |
| 155 | return NULL; |
| 156 | |
| 157 | skb_reserve(buf, hdr_len); |
| 158 | |
| 159 | return buf; |
| 160 | } |
| 161 | |
| 162 | static struct sk_buff *tipc_get_err_tlv(char *str) |
| 163 | { |
| 164 | int str_len = strlen(str) + 1; |
| 165 | struct sk_buff *buf; |
| 166 | |
| 167 | buf = tipc_tlv_alloc(TLV_SPACE(str_len)); |
| 168 | if (buf) |
| 169 | tipc_add_tlv(buf, TIPC_TLV_ERROR_STRING, str, str_len); |
| 170 | |
| 171 | return buf; |
| 172 | } |
| 173 | |
Ying Xue | 8b66fee | 2019-01-14 17:22:25 +0800 | [diff] [blame] | 174 | static inline bool string_is_valid(char *s, int len) |
| 175 | { |
| 176 | return memchr(s, '\0', len) ? true : false; |
| 177 | } |
| 178 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 179 | static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd, |
| 180 | struct tipc_nl_compat_msg *msg, |
| 181 | struct sk_buff *arg) |
| 182 | { |
| 183 | int len = 0; |
| 184 | int err; |
| 185 | struct sk_buff *buf; |
| 186 | struct nlmsghdr *nlmsg; |
| 187 | struct netlink_callback cb; |
| 188 | |
| 189 | memset(&cb, 0, sizeof(cb)); |
| 190 | cb.nlh = (struct nlmsghdr *)arg->data; |
| 191 | cb.skb = arg; |
| 192 | |
| 193 | buf = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 194 | if (!buf) |
| 195 | return -ENOMEM; |
| 196 | |
| 197 | buf->sk = msg->dst_sk; |
Cong Wang | 12a78b0 | 2018-09-11 15:12:17 -0700 | [diff] [blame] | 198 | if (__tipc_dump_start(&cb, msg->net)) { |
| 199 | kfree_skb(buf); |
| 200 | return -ENOMEM; |
| 201 | } |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 202 | |
| 203 | do { |
| 204 | int rem; |
| 205 | |
| 206 | len = (*cmd->dumpit)(buf, &cb); |
| 207 | |
| 208 | nlmsg_for_each_msg(nlmsg, nlmsg_hdr(buf), len, rem) { |
| 209 | struct nlattr **attrs; |
| 210 | |
| 211 | err = tipc_nlmsg_parse(nlmsg, &attrs); |
| 212 | if (err) |
| 213 | goto err_out; |
| 214 | |
| 215 | err = (*cmd->format)(msg, attrs); |
| 216 | if (err) |
| 217 | goto err_out; |
| 218 | |
| 219 | if (tipc_skb_tailroom(msg->rep) <= 1) { |
| 220 | err = -EMSGSIZE; |
| 221 | goto err_out; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | skb_reset_tail_pointer(buf); |
| 226 | buf->len = 0; |
| 227 | |
| 228 | } while (len); |
| 229 | |
| 230 | err = 0; |
| 231 | |
| 232 | err_out: |
Cong Wang | 8f5c5fc | 2018-09-04 14:54:55 -0700 | [diff] [blame] | 233 | tipc_dump_done(&cb); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 234 | kfree_skb(buf); |
| 235 | |
| 236 | if (err == -EMSGSIZE) { |
| 237 | /* The legacy API only considered messages filling |
| 238 | * "ULTRA_STRING_MAX_LEN" to be truncated. |
| 239 | */ |
| 240 | if ((TIPC_SKB_MAX - msg->rep->len) <= 1) { |
| 241 | char *tail = skb_tail_pointer(msg->rep); |
| 242 | |
| 243 | if (*tail != '\0') |
| 244 | sprintf(tail - sizeof(REPLY_TRUNCATED) - 1, |
| 245 | REPLY_TRUNCATED); |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | return err; |
| 252 | } |
| 253 | |
| 254 | static int tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd, |
| 255 | struct tipc_nl_compat_msg *msg) |
| 256 | { |
| 257 | int err; |
| 258 | struct sk_buff *arg; |
| 259 | |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 260 | if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type)) |
| 261 | return -EINVAL; |
| 262 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 263 | msg->rep = tipc_tlv_alloc(msg->rep_size); |
| 264 | if (!msg->rep) |
| 265 | return -ENOMEM; |
| 266 | |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 267 | if (msg->rep_type) |
| 268 | tipc_tlv_init(msg->rep, msg->rep_type); |
| 269 | |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 270 | if (cmd->header) |
| 271 | (*cmd->header)(msg); |
| 272 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 273 | arg = nlmsg_new(0, GFP_KERNEL); |
| 274 | if (!arg) { |
| 275 | kfree_skb(msg->rep); |
Eric Dumazet | 5bfd37b | 2017-08-16 09:41:54 -0700 | [diff] [blame] | 276 | msg->rep = NULL; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 277 | return -ENOMEM; |
| 278 | } |
| 279 | |
| 280 | err = __tipc_nl_compat_dumpit(cmd, msg, arg); |
Eric Dumazet | 5bfd37b | 2017-08-16 09:41:54 -0700 | [diff] [blame] | 281 | if (err) { |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 282 | kfree_skb(msg->rep); |
Eric Dumazet | 5bfd37b | 2017-08-16 09:41:54 -0700 | [diff] [blame] | 283 | msg->rep = NULL; |
| 284 | } |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 285 | kfree_skb(arg); |
| 286 | |
| 287 | return err; |
| 288 | } |
| 289 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 290 | static int __tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd, |
| 291 | struct tipc_nl_compat_msg *msg) |
| 292 | { |
| 293 | int err; |
| 294 | struct sk_buff *doit_buf; |
| 295 | struct sk_buff *trans_buf; |
| 296 | struct nlattr **attrbuf; |
| 297 | struct genl_info info; |
| 298 | |
| 299 | trans_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 300 | if (!trans_buf) |
| 301 | return -ENOMEM; |
| 302 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 303 | attrbuf = kmalloc_array(tipc_genl_family.maxattr + 1, |
| 304 | sizeof(struct nlattr *), |
| 305 | GFP_KERNEL); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 306 | if (!attrbuf) { |
| 307 | err = -ENOMEM; |
| 308 | goto trans_out; |
| 309 | } |
| 310 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 311 | doit_buf = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 312 | if (!doit_buf) { |
| 313 | err = -ENOMEM; |
Ying Xue | e5d1a1e | 2018-02-14 13:37:58 +0800 | [diff] [blame] | 314 | goto attrbuf_out; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 317 | memset(&info, 0, sizeof(info)); |
| 318 | info.attrs = attrbuf; |
| 319 | |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 320 | rtnl_lock(); |
Ying Xue | e5d1a1e | 2018-02-14 13:37:58 +0800 | [diff] [blame] | 321 | err = (*cmd->transcode)(cmd, trans_buf, msg); |
| 322 | if (err) |
| 323 | goto doit_out; |
| 324 | |
| 325 | err = nla_parse(attrbuf, tipc_genl_family.maxattr, |
| 326 | (const struct nlattr *)trans_buf->data, |
| 327 | trans_buf->len, NULL, NULL); |
| 328 | if (err) |
| 329 | goto doit_out; |
| 330 | |
| 331 | doit_buf->sk = msg->dst_sk; |
| 332 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 333 | err = (*cmd->doit)(doit_buf, &info); |
Ying Xue | e5d1a1e | 2018-02-14 13:37:58 +0800 | [diff] [blame] | 334 | doit_out: |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 335 | rtnl_unlock(); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 336 | |
| 337 | kfree_skb(doit_buf); |
Ying Xue | e5d1a1e | 2018-02-14 13:37:58 +0800 | [diff] [blame] | 338 | attrbuf_out: |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 339 | kfree(attrbuf); |
| 340 | trans_out: |
| 341 | kfree_skb(trans_buf); |
| 342 | |
| 343 | return err; |
| 344 | } |
| 345 | |
| 346 | static int tipc_nl_compat_doit(struct tipc_nl_compat_cmd_doit *cmd, |
| 347 | struct tipc_nl_compat_msg *msg) |
| 348 | { |
| 349 | int err; |
| 350 | |
| 351 | if (msg->req_type && !TLV_CHECK_TYPE(msg->req, msg->req_type)) |
| 352 | return -EINVAL; |
| 353 | |
| 354 | err = __tipc_nl_compat_doit(cmd, msg); |
| 355 | if (err) |
| 356 | return err; |
| 357 | |
| 358 | /* The legacy API considered an empty message a success message */ |
| 359 | msg->rep = tipc_tlv_alloc(0); |
| 360 | if (!msg->rep) |
| 361 | return -ENOMEM; |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 366 | static int tipc_nl_compat_bearer_dump(struct tipc_nl_compat_msg *msg, |
| 367 | struct nlattr **attrs) |
| 368 | { |
| 369 | struct nlattr *bearer[TIPC_NLA_BEARER_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 370 | int err; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 371 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 372 | if (!attrs[TIPC_NLA_BEARER]) |
| 373 | return -EINVAL; |
| 374 | |
| 375 | err = nla_parse_nested(bearer, TIPC_NLA_BEARER_MAX, |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 376 | attrs[TIPC_NLA_BEARER], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 377 | if (err) |
| 378 | return err; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 379 | |
| 380 | return tipc_add_tlv(msg->rep, TIPC_TLV_BEARER_NAME, |
| 381 | nla_data(bearer[TIPC_NLA_BEARER_NAME]), |
| 382 | nla_len(bearer[TIPC_NLA_BEARER_NAME])); |
| 383 | } |
| 384 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 385 | static int tipc_nl_compat_bearer_enable(struct tipc_nl_compat_cmd_doit *cmd, |
| 386 | struct sk_buff *skb, |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 387 | struct tipc_nl_compat_msg *msg) |
| 388 | { |
| 389 | struct nlattr *prop; |
| 390 | struct nlattr *bearer; |
| 391 | struct tipc_bearer_config *b; |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 392 | int len; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 393 | |
| 394 | b = (struct tipc_bearer_config *)TLV_DATA(msg->req); |
| 395 | |
| 396 | bearer = nla_nest_start(skb, TIPC_NLA_BEARER); |
| 397 | if (!bearer) |
| 398 | return -EMSGSIZE; |
| 399 | |
Xin Long | 6f07e5f | 2019-03-31 22:50:08 +0800 | [diff] [blame] | 400 | len = TLV_GET_DATA_LEN(msg->req); |
| 401 | len -= offsetof(struct tipc_bearer_config, name); |
| 402 | if (len <= 0) |
| 403 | return -EINVAL; |
| 404 | |
| 405 | len = min_t(int, len, TIPC_MAX_BEARER_NAME); |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 406 | if (!string_is_valid(b->name, len)) |
| 407 | return -EINVAL; |
| 408 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 409 | if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, b->name)) |
| 410 | return -EMSGSIZE; |
| 411 | |
| 412 | if (nla_put_u32(skb, TIPC_NLA_BEARER_DOMAIN, ntohl(b->disc_domain))) |
| 413 | return -EMSGSIZE; |
| 414 | |
| 415 | if (ntohl(b->priority) <= TIPC_MAX_LINK_PRI) { |
| 416 | prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP); |
| 417 | if (!prop) |
| 418 | return -EMSGSIZE; |
| 419 | if (nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(b->priority))) |
| 420 | return -EMSGSIZE; |
| 421 | nla_nest_end(skb, prop); |
| 422 | } |
| 423 | nla_nest_end(skb, bearer); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 428 | static int tipc_nl_compat_bearer_disable(struct tipc_nl_compat_cmd_doit *cmd, |
| 429 | struct sk_buff *skb, |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 430 | struct tipc_nl_compat_msg *msg) |
| 431 | { |
| 432 | char *name; |
| 433 | struct nlattr *bearer; |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 434 | int len; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 435 | |
| 436 | name = (char *)TLV_DATA(msg->req); |
| 437 | |
| 438 | bearer = nla_nest_start(skb, TIPC_NLA_BEARER); |
| 439 | if (!bearer) |
| 440 | return -EMSGSIZE; |
| 441 | |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 442 | len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_BEARER_NAME); |
| 443 | if (!string_is_valid(name, len)) |
| 444 | return -EINVAL; |
| 445 | |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 446 | if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, name)) |
| 447 | return -EMSGSIZE; |
| 448 | |
| 449 | nla_nest_end(skb, bearer); |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 454 | static inline u32 perc(u32 count, u32 total) |
| 455 | { |
| 456 | return (count * 100 + (total / 2)) / total; |
| 457 | } |
| 458 | |
| 459 | static void __fill_bc_link_stat(struct tipc_nl_compat_msg *msg, |
| 460 | struct nlattr *prop[], struct nlattr *stats[]) |
| 461 | { |
| 462 | tipc_tlv_sprintf(msg->rep, " Window:%u packets\n", |
| 463 | nla_get_u32(prop[TIPC_NLA_PROP_WIN])); |
| 464 | |
| 465 | tipc_tlv_sprintf(msg->rep, |
| 466 | " RX packets:%u fragments:%u/%u bundles:%u/%u\n", |
| 467 | nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]), |
| 468 | nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]), |
| 469 | nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]), |
| 470 | nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]), |
| 471 | nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED])); |
| 472 | |
| 473 | tipc_tlv_sprintf(msg->rep, |
| 474 | " TX packets:%u fragments:%u/%u bundles:%u/%u\n", |
| 475 | nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]), |
| 476 | nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]), |
| 477 | nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]), |
| 478 | nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]), |
| 479 | nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED])); |
| 480 | |
| 481 | tipc_tlv_sprintf(msg->rep, " RX naks:%u defs:%u dups:%u\n", |
| 482 | nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]), |
| 483 | nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]), |
| 484 | nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES])); |
| 485 | |
| 486 | tipc_tlv_sprintf(msg->rep, " TX naks:%u acks:%u dups:%u\n", |
| 487 | nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]), |
| 488 | nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]), |
| 489 | nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED])); |
| 490 | |
| 491 | tipc_tlv_sprintf(msg->rep, |
| 492 | " Congestion link:%u Send queue max:%u avg:%u", |
| 493 | nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]), |
| 494 | nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]), |
| 495 | nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE])); |
| 496 | } |
| 497 | |
| 498 | static int tipc_nl_compat_link_stat_dump(struct tipc_nl_compat_msg *msg, |
| 499 | struct nlattr **attrs) |
| 500 | { |
| 501 | char *name; |
| 502 | struct nlattr *link[TIPC_NLA_LINK_MAX + 1]; |
| 503 | struct nlattr *prop[TIPC_NLA_PROP_MAX + 1]; |
| 504 | struct nlattr *stats[TIPC_NLA_STATS_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 505 | int err; |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 506 | int len; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 507 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 508 | if (!attrs[TIPC_NLA_LINK]) |
| 509 | return -EINVAL; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 510 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 511 | err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 512 | NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 513 | if (err) |
| 514 | return err; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 515 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 516 | if (!link[TIPC_NLA_LINK_PROP]) |
| 517 | return -EINVAL; |
| 518 | |
| 519 | err = nla_parse_nested(prop, TIPC_NLA_PROP_MAX, |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 520 | link[TIPC_NLA_LINK_PROP], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 521 | if (err) |
| 522 | return err; |
| 523 | |
| 524 | if (!link[TIPC_NLA_LINK_STATS]) |
| 525 | return -EINVAL; |
| 526 | |
| 527 | err = nla_parse_nested(stats, TIPC_NLA_STATS_MAX, |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 528 | link[TIPC_NLA_LINK_STATS], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 529 | if (err) |
| 530 | return err; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 531 | |
| 532 | name = (char *)TLV_DATA(msg->req); |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 533 | |
| 534 | len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME); |
| 535 | if (!string_is_valid(name, len)) |
| 536 | return -EINVAL; |
| 537 | |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 538 | if (strcmp(name, nla_data(link[TIPC_NLA_LINK_NAME])) != 0) |
| 539 | return 0; |
| 540 | |
| 541 | tipc_tlv_sprintf(msg->rep, "\nLink <%s>\n", |
| 542 | nla_data(link[TIPC_NLA_LINK_NAME])); |
| 543 | |
| 544 | if (link[TIPC_NLA_LINK_BROADCAST]) { |
| 545 | __fill_bc_link_stat(msg, prop, stats); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | if (link[TIPC_NLA_LINK_ACTIVE]) |
| 550 | tipc_tlv_sprintf(msg->rep, " ACTIVE"); |
| 551 | else if (link[TIPC_NLA_LINK_UP]) |
| 552 | tipc_tlv_sprintf(msg->rep, " STANDBY"); |
| 553 | else |
| 554 | tipc_tlv_sprintf(msg->rep, " DEFUNCT"); |
| 555 | |
| 556 | tipc_tlv_sprintf(msg->rep, " MTU:%u Priority:%u", |
| 557 | nla_get_u32(link[TIPC_NLA_LINK_MTU]), |
| 558 | nla_get_u32(prop[TIPC_NLA_PROP_PRIO])); |
| 559 | |
| 560 | tipc_tlv_sprintf(msg->rep, " Tolerance:%u ms Window:%u packets\n", |
| 561 | nla_get_u32(prop[TIPC_NLA_PROP_TOL]), |
| 562 | nla_get_u32(prop[TIPC_NLA_PROP_WIN])); |
| 563 | |
| 564 | tipc_tlv_sprintf(msg->rep, |
| 565 | " RX packets:%u fragments:%u/%u bundles:%u/%u\n", |
| 566 | nla_get_u32(link[TIPC_NLA_LINK_RX]) - |
| 567 | nla_get_u32(stats[TIPC_NLA_STATS_RX_INFO]), |
| 568 | nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]), |
| 569 | nla_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]), |
| 570 | nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]), |
| 571 | nla_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED])); |
| 572 | |
| 573 | tipc_tlv_sprintf(msg->rep, |
| 574 | " TX packets:%u fragments:%u/%u bundles:%u/%u\n", |
| 575 | nla_get_u32(link[TIPC_NLA_LINK_TX]) - |
| 576 | nla_get_u32(stats[TIPC_NLA_STATS_TX_INFO]), |
| 577 | nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]), |
| 578 | nla_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]), |
| 579 | nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]), |
| 580 | nla_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED])); |
| 581 | |
| 582 | tipc_tlv_sprintf(msg->rep, |
| 583 | " TX profile sample:%u packets average:%u octets\n", |
| 584 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]), |
| 585 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) / |
| 586 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])); |
| 587 | |
| 588 | tipc_tlv_sprintf(msg->rep, |
| 589 | " 0-64:%u%% -256:%u%% -1024:%u%% -4096:%u%% ", |
| 590 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]), |
| 591 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])), |
| 592 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]), |
| 593 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])), |
| 594 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]), |
| 595 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])), |
| 596 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]), |
| 597 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]))); |
| 598 | |
| 599 | tipc_tlv_sprintf(msg->rep, "-16384:%u%% -32768:%u%% -66000:%u%%\n", |
| 600 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]), |
| 601 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])), |
| 602 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]), |
| 603 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT])), |
| 604 | perc(nla_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]), |
| 605 | nla_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]))); |
| 606 | |
| 607 | tipc_tlv_sprintf(msg->rep, |
| 608 | " RX states:%u probes:%u naks:%u defs:%u dups:%u\n", |
| 609 | nla_get_u32(stats[TIPC_NLA_STATS_RX_STATES]), |
| 610 | nla_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]), |
| 611 | nla_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]), |
| 612 | nla_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]), |
| 613 | nla_get_u32(stats[TIPC_NLA_STATS_DUPLICATES])); |
| 614 | |
| 615 | tipc_tlv_sprintf(msg->rep, |
| 616 | " TX states:%u probes:%u naks:%u acks:%u dups:%u\n", |
| 617 | nla_get_u32(stats[TIPC_NLA_STATS_TX_STATES]), |
| 618 | nla_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]), |
| 619 | nla_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]), |
| 620 | nla_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]), |
| 621 | nla_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED])); |
| 622 | |
| 623 | tipc_tlv_sprintf(msg->rep, |
| 624 | " Congestion link:%u Send queue max:%u avg:%u", |
| 625 | nla_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]), |
| 626 | nla_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]), |
| 627 | nla_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE])); |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 632 | static int tipc_nl_compat_link_dump(struct tipc_nl_compat_msg *msg, |
| 633 | struct nlattr **attrs) |
| 634 | { |
| 635 | struct nlattr *link[TIPC_NLA_LINK_MAX + 1]; |
| 636 | struct tipc_link_info link_info; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 637 | int err; |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 638 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 639 | if (!attrs[TIPC_NLA_LINK]) |
| 640 | return -EINVAL; |
| 641 | |
| 642 | err = nla_parse_nested(link, TIPC_NLA_LINK_MAX, attrs[TIPC_NLA_LINK], |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 643 | NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 644 | if (err) |
| 645 | return err; |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 646 | |
| 647 | link_info.dest = nla_get_flag(link[TIPC_NLA_LINK_DEST]); |
| 648 | link_info.up = htonl(nla_get_flag(link[TIPC_NLA_LINK_UP])); |
Richard Alpe | 55e77a3 | 2016-07-01 11:11:21 +0200 | [diff] [blame] | 649 | nla_strlcpy(link_info.str, link[TIPC_NLA_LINK_NAME], |
Kangjie Lu | 5d2be14 | 2016-06-02 04:04:56 -0400 | [diff] [blame] | 650 | TIPC_MAX_LINK_NAME); |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 651 | |
| 652 | return tipc_add_tlv(msg->rep, TIPC_TLV_LINK_INFO, |
| 653 | &link_info, sizeof(link_info)); |
| 654 | } |
| 655 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 656 | static int __tipc_add_link_prop(struct sk_buff *skb, |
| 657 | struct tipc_nl_compat_msg *msg, |
| 658 | struct tipc_link_config *lc) |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 659 | { |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 660 | switch (msg->cmd) { |
| 661 | case TIPC_CMD_SET_LINK_PRI: |
| 662 | return nla_put_u32(skb, TIPC_NLA_PROP_PRIO, ntohl(lc->value)); |
| 663 | case TIPC_CMD_SET_LINK_TOL: |
| 664 | return nla_put_u32(skb, TIPC_NLA_PROP_TOL, ntohl(lc->value)); |
| 665 | case TIPC_CMD_SET_LINK_WINDOW: |
| 666 | return nla_put_u32(skb, TIPC_NLA_PROP_WIN, ntohl(lc->value)); |
| 667 | } |
| 668 | |
| 669 | return -EINVAL; |
| 670 | } |
| 671 | |
| 672 | static int tipc_nl_compat_media_set(struct sk_buff *skb, |
| 673 | struct tipc_nl_compat_msg *msg) |
| 674 | { |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 675 | struct nlattr *prop; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 676 | struct nlattr *media; |
| 677 | struct tipc_link_config *lc; |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 678 | int len; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 679 | |
| 680 | lc = (struct tipc_link_config *)TLV_DATA(msg->req); |
| 681 | |
| 682 | media = nla_nest_start(skb, TIPC_NLA_MEDIA); |
| 683 | if (!media) |
| 684 | return -EMSGSIZE; |
| 685 | |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 686 | len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME); |
| 687 | if (!string_is_valid(lc->name, len)) |
| 688 | return -EINVAL; |
| 689 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 690 | if (nla_put_string(skb, TIPC_NLA_MEDIA_NAME, lc->name)) |
| 691 | return -EMSGSIZE; |
| 692 | |
| 693 | prop = nla_nest_start(skb, TIPC_NLA_MEDIA_PROP); |
| 694 | if (!prop) |
| 695 | return -EMSGSIZE; |
| 696 | |
| 697 | __tipc_add_link_prop(skb, msg, lc); |
| 698 | nla_nest_end(skb, prop); |
| 699 | nla_nest_end(skb, media); |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | static int tipc_nl_compat_bearer_set(struct sk_buff *skb, |
| 705 | struct tipc_nl_compat_msg *msg) |
| 706 | { |
| 707 | struct nlattr *prop; |
| 708 | struct nlattr *bearer; |
| 709 | struct tipc_link_config *lc; |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 710 | int len; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 711 | |
| 712 | lc = (struct tipc_link_config *)TLV_DATA(msg->req); |
| 713 | |
| 714 | bearer = nla_nest_start(skb, TIPC_NLA_BEARER); |
| 715 | if (!bearer) |
| 716 | return -EMSGSIZE; |
| 717 | |
Ying Xue | 0762216 | 2019-01-14 17:22:26 +0800 | [diff] [blame] | 718 | len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_MEDIA_NAME); |
| 719 | if (!string_is_valid(lc->name, len)) |
| 720 | return -EINVAL; |
| 721 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 722 | if (nla_put_string(skb, TIPC_NLA_BEARER_NAME, lc->name)) |
| 723 | return -EMSGSIZE; |
| 724 | |
| 725 | prop = nla_nest_start(skb, TIPC_NLA_BEARER_PROP); |
| 726 | if (!prop) |
| 727 | return -EMSGSIZE; |
| 728 | |
| 729 | __tipc_add_link_prop(skb, msg, lc); |
| 730 | nla_nest_end(skb, prop); |
| 731 | nla_nest_end(skb, bearer); |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static int __tipc_nl_compat_link_set(struct sk_buff *skb, |
| 737 | struct tipc_nl_compat_msg *msg) |
| 738 | { |
| 739 | struct nlattr *prop; |
| 740 | struct nlattr *link; |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 741 | struct tipc_link_config *lc; |
| 742 | |
| 743 | lc = (struct tipc_link_config *)TLV_DATA(msg->req); |
| 744 | |
| 745 | link = nla_nest_start(skb, TIPC_NLA_LINK); |
| 746 | if (!link) |
| 747 | return -EMSGSIZE; |
| 748 | |
| 749 | if (nla_put_string(skb, TIPC_NLA_LINK_NAME, lc->name)) |
| 750 | return -EMSGSIZE; |
| 751 | |
| 752 | prop = nla_nest_start(skb, TIPC_NLA_LINK_PROP); |
| 753 | if (!prop) |
| 754 | return -EMSGSIZE; |
| 755 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 756 | __tipc_add_link_prop(skb, msg, lc); |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 757 | nla_nest_end(skb, prop); |
| 758 | nla_nest_end(skb, link); |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 763 | static int tipc_nl_compat_link_set(struct tipc_nl_compat_cmd_doit *cmd, |
| 764 | struct sk_buff *skb, |
| 765 | struct tipc_nl_compat_msg *msg) |
| 766 | { |
| 767 | struct tipc_link_config *lc; |
| 768 | struct tipc_bearer *bearer; |
| 769 | struct tipc_media *media; |
Ying Xue | edf5ff0 | 2019-01-14 17:22:27 +0800 | [diff] [blame] | 770 | int len; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 771 | |
| 772 | lc = (struct tipc_link_config *)TLV_DATA(msg->req); |
| 773 | |
Xin Long | 8c63bf9 | 2019-03-31 22:50:09 +0800 | [diff] [blame^] | 774 | len = TLV_GET_DATA_LEN(msg->req); |
| 775 | len -= offsetof(struct tipc_link_config, name); |
| 776 | if (len <= 0) |
| 777 | return -EINVAL; |
| 778 | |
| 779 | len = min_t(int, len, TIPC_MAX_LINK_NAME); |
Ying Xue | edf5ff0 | 2019-01-14 17:22:27 +0800 | [diff] [blame] | 780 | if (!string_is_valid(lc->name, len)) |
| 781 | return -EINVAL; |
| 782 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 783 | media = tipc_media_find(lc->name); |
| 784 | if (media) { |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 785 | cmd->doit = &__tipc_nl_media_set; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 786 | return tipc_nl_compat_media_set(skb, msg); |
| 787 | } |
| 788 | |
| 789 | bearer = tipc_bearer_find(msg->net, lc->name); |
| 790 | if (bearer) { |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 791 | cmd->doit = &__tipc_nl_bearer_set; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 792 | return tipc_nl_compat_bearer_set(skb, msg); |
| 793 | } |
| 794 | |
| 795 | return __tipc_nl_compat_link_set(skb, msg); |
| 796 | } |
| 797 | |
| 798 | static int tipc_nl_compat_link_reset_stats(struct tipc_nl_compat_cmd_doit *cmd, |
| 799 | struct sk_buff *skb, |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 800 | struct tipc_nl_compat_msg *msg) |
| 801 | { |
| 802 | char *name; |
| 803 | struct nlattr *link; |
Ying Xue | 8b66fee | 2019-01-14 17:22:25 +0800 | [diff] [blame] | 804 | int len; |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 805 | |
| 806 | name = (char *)TLV_DATA(msg->req); |
| 807 | |
| 808 | link = nla_nest_start(skb, TIPC_NLA_LINK); |
| 809 | if (!link) |
| 810 | return -EMSGSIZE; |
| 811 | |
Ying Xue | 8b66fee | 2019-01-14 17:22:25 +0800 | [diff] [blame] | 812 | len = min_t(int, TLV_GET_DATA_LEN(msg->req), TIPC_MAX_LINK_NAME); |
| 813 | if (!string_is_valid(name, len)) |
| 814 | return -EINVAL; |
| 815 | |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 816 | if (nla_put_string(skb, TIPC_NLA_LINK_NAME, name)) |
| 817 | return -EMSGSIZE; |
| 818 | |
| 819 | nla_nest_end(skb, link); |
| 820 | |
| 821 | return 0; |
| 822 | } |
| 823 | |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 824 | static int tipc_nl_compat_name_table_dump_header(struct tipc_nl_compat_msg *msg) |
| 825 | { |
| 826 | int i; |
| 827 | u32 depth; |
| 828 | struct tipc_name_table_query *ntq; |
| 829 | static const char * const header[] = { |
| 830 | "Type ", |
| 831 | "Lower Upper ", |
| 832 | "Port Identity ", |
| 833 | "Publication Scope" |
| 834 | }; |
| 835 | |
| 836 | ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req); |
Ying Xue | 974cb0e | 2019-01-14 17:22:28 +0800 | [diff] [blame] | 837 | if (TLV_GET_DATA_LEN(msg->req) < sizeof(struct tipc_name_table_query)) |
| 838 | return -EINVAL; |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 839 | |
| 840 | depth = ntohl(ntq->depth); |
| 841 | |
| 842 | if (depth > 4) |
| 843 | depth = 4; |
| 844 | for (i = 0; i < depth; i++) |
| 845 | tipc_tlv_sprintf(msg->rep, header[i]); |
| 846 | tipc_tlv_sprintf(msg->rep, "\n"); |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static int tipc_nl_compat_name_table_dump(struct tipc_nl_compat_msg *msg, |
| 852 | struct nlattr **attrs) |
| 853 | { |
| 854 | char port_str[27]; |
| 855 | struct tipc_name_table_query *ntq; |
| 856 | struct nlattr *nt[TIPC_NLA_NAME_TABLE_MAX + 1]; |
| 857 | struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1]; |
| 858 | u32 node, depth, type, lowbound, upbound; |
| 859 | static const char * const scope_str[] = {"", " zone", " cluster", |
| 860 | " node"}; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 861 | int err; |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 862 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 863 | if (!attrs[TIPC_NLA_NAME_TABLE]) |
| 864 | return -EINVAL; |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 865 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 866 | err = nla_parse_nested(nt, TIPC_NLA_NAME_TABLE_MAX, |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 867 | attrs[TIPC_NLA_NAME_TABLE], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 868 | if (err) |
| 869 | return err; |
| 870 | |
| 871 | if (!nt[TIPC_NLA_NAME_TABLE_PUBL]) |
| 872 | return -EINVAL; |
| 873 | |
| 874 | err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 875 | nt[TIPC_NLA_NAME_TABLE_PUBL], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 876 | if (err) |
| 877 | return err; |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 878 | |
| 879 | ntq = (struct tipc_name_table_query *)TLV_DATA(msg->req); |
| 880 | |
| 881 | depth = ntohl(ntq->depth); |
| 882 | type = ntohl(ntq->type); |
| 883 | lowbound = ntohl(ntq->lowbound); |
| 884 | upbound = ntohl(ntq->upbound); |
| 885 | |
| 886 | if (!(depth & TIPC_NTQ_ALLTYPES) && |
| 887 | (type != nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]))) |
| 888 | return 0; |
| 889 | if (lowbound && (lowbound > nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]))) |
| 890 | return 0; |
| 891 | if (upbound && (upbound < nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]))) |
| 892 | return 0; |
| 893 | |
| 894 | tipc_tlv_sprintf(msg->rep, "%-10u ", |
| 895 | nla_get_u32(publ[TIPC_NLA_PUBL_TYPE])); |
| 896 | |
| 897 | if (depth == 1) |
| 898 | goto out; |
| 899 | |
| 900 | tipc_tlv_sprintf(msg->rep, "%-10u %-10u ", |
| 901 | nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]), |
| 902 | nla_get_u32(publ[TIPC_NLA_PUBL_UPPER])); |
| 903 | |
| 904 | if (depth == 2) |
| 905 | goto out; |
| 906 | |
| 907 | node = nla_get_u32(publ[TIPC_NLA_PUBL_NODE]); |
| 908 | sprintf(port_str, "<%u.%u.%u:%u>", tipc_zone(node), tipc_cluster(node), |
| 909 | tipc_node(node), nla_get_u32(publ[TIPC_NLA_PUBL_REF])); |
| 910 | tipc_tlv_sprintf(msg->rep, "%-26s ", port_str); |
| 911 | |
| 912 | if (depth == 3) |
| 913 | goto out; |
| 914 | |
| 915 | tipc_tlv_sprintf(msg->rep, "%-10u %s", |
Richard Alpe | 03aaaa9 | 2016-05-17 16:57:37 +0200 | [diff] [blame] | 916 | nla_get_u32(publ[TIPC_NLA_PUBL_KEY]), |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 917 | scope_str[nla_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]); |
| 918 | out: |
| 919 | tipc_tlv_sprintf(msg->rep, "\n"); |
| 920 | |
| 921 | return 0; |
| 922 | } |
| 923 | |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 924 | static int __tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, |
| 925 | struct nlattr **attrs) |
| 926 | { |
| 927 | u32 type, lower, upper; |
| 928 | struct nlattr *publ[TIPC_NLA_PUBL_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 929 | int err; |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 930 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 931 | if (!attrs[TIPC_NLA_PUBL]) |
| 932 | return -EINVAL; |
| 933 | |
| 934 | err = nla_parse_nested(publ, TIPC_NLA_PUBL_MAX, attrs[TIPC_NLA_PUBL], |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 935 | NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 936 | if (err) |
| 937 | return err; |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 938 | |
| 939 | type = nla_get_u32(publ[TIPC_NLA_PUBL_TYPE]); |
| 940 | lower = nla_get_u32(publ[TIPC_NLA_PUBL_LOWER]); |
| 941 | upper = nla_get_u32(publ[TIPC_NLA_PUBL_UPPER]); |
| 942 | |
| 943 | if (lower == upper) |
| 944 | tipc_tlv_sprintf(msg->rep, " {%u,%u}", type, lower); |
| 945 | else |
| 946 | tipc_tlv_sprintf(msg->rep, " {%u,%u,%u}", type, lower, upper); |
| 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static int tipc_nl_compat_publ_dump(struct tipc_nl_compat_msg *msg, u32 sock) |
| 952 | { |
| 953 | int err; |
| 954 | void *hdr; |
| 955 | struct nlattr *nest; |
| 956 | struct sk_buff *args; |
| 957 | struct tipc_nl_compat_cmd_dump dump; |
| 958 | |
| 959 | args = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 960 | if (!args) |
| 961 | return -ENOMEM; |
| 962 | |
| 963 | hdr = genlmsg_put(args, 0, 0, &tipc_genl_family, NLM_F_MULTI, |
| 964 | TIPC_NL_PUBL_GET); |
Gustavo A. R. Silva | f87d8ad | 2019-01-05 10:52:23 -0600 | [diff] [blame] | 965 | if (!hdr) { |
| 966 | kfree_skb(args); |
Kangjie Lu | 46273cf | 2018-12-26 00:09:04 -0600 | [diff] [blame] | 967 | return -EMSGSIZE; |
Gustavo A. R. Silva | f87d8ad | 2019-01-05 10:52:23 -0600 | [diff] [blame] | 968 | } |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 969 | |
| 970 | nest = nla_nest_start(args, TIPC_NLA_SOCK); |
| 971 | if (!nest) { |
| 972 | kfree_skb(args); |
| 973 | return -EMSGSIZE; |
| 974 | } |
| 975 | |
| 976 | if (nla_put_u32(args, TIPC_NLA_SOCK_REF, sock)) { |
| 977 | kfree_skb(args); |
| 978 | return -EMSGSIZE; |
| 979 | } |
| 980 | |
| 981 | nla_nest_end(args, nest); |
| 982 | genlmsg_end(args, hdr); |
| 983 | |
| 984 | dump.dumpit = tipc_nl_publ_dump; |
| 985 | dump.format = __tipc_nl_compat_publ_dump; |
| 986 | |
| 987 | err = __tipc_nl_compat_dumpit(&dump, msg, args); |
| 988 | |
| 989 | kfree_skb(args); |
| 990 | |
| 991 | return err; |
| 992 | } |
| 993 | |
| 994 | static int tipc_nl_compat_sk_dump(struct tipc_nl_compat_msg *msg, |
| 995 | struct nlattr **attrs) |
| 996 | { |
| 997 | int err; |
| 998 | u32 sock_ref; |
| 999 | struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1]; |
| 1000 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1001 | if (!attrs[TIPC_NLA_SOCK]) |
| 1002 | return -EINVAL; |
| 1003 | |
| 1004 | err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX, attrs[TIPC_NLA_SOCK], |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1005 | NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1006 | if (err) |
| 1007 | return err; |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 1008 | |
| 1009 | sock_ref = nla_get_u32(sock[TIPC_NLA_SOCK_REF]); |
| 1010 | tipc_tlv_sprintf(msg->rep, "%u:", sock_ref); |
| 1011 | |
| 1012 | if (sock[TIPC_NLA_SOCK_CON]) { |
| 1013 | u32 node; |
| 1014 | struct nlattr *con[TIPC_NLA_CON_MAX + 1]; |
| 1015 | |
Aditya Pakki | 89dfd00 | 2018-12-23 18:54:53 -0600 | [diff] [blame] | 1016 | err = nla_parse_nested(con, TIPC_NLA_CON_MAX, |
| 1017 | sock[TIPC_NLA_SOCK_CON], NULL, NULL); |
| 1018 | |
| 1019 | if (err) |
| 1020 | return err; |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 1021 | |
| 1022 | node = nla_get_u32(con[TIPC_NLA_CON_NODE]); |
| 1023 | tipc_tlv_sprintf(msg->rep, " connected to <%u.%u.%u:%u>", |
| 1024 | tipc_zone(node), |
| 1025 | tipc_cluster(node), |
| 1026 | tipc_node(node), |
| 1027 | nla_get_u32(con[TIPC_NLA_CON_SOCK])); |
| 1028 | |
| 1029 | if (con[TIPC_NLA_CON_FLAG]) |
| 1030 | tipc_tlv_sprintf(msg->rep, " via {%u,%u}\n", |
| 1031 | nla_get_u32(con[TIPC_NLA_CON_TYPE]), |
| 1032 | nla_get_u32(con[TIPC_NLA_CON_INST])); |
| 1033 | else |
| 1034 | tipc_tlv_sprintf(msg->rep, "\n"); |
| 1035 | } else if (sock[TIPC_NLA_SOCK_HAS_PUBL]) { |
| 1036 | tipc_tlv_sprintf(msg->rep, " bound to"); |
| 1037 | |
| 1038 | err = tipc_nl_compat_publ_dump(msg, sock_ref); |
| 1039 | if (err) |
| 1040 | return err; |
| 1041 | } |
| 1042 | tipc_tlv_sprintf(msg->rep, "\n"); |
| 1043 | |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
Richard Alpe | 5bfc335 | 2015-02-09 09:50:12 +0100 | [diff] [blame] | 1047 | static int tipc_nl_compat_media_dump(struct tipc_nl_compat_msg *msg, |
| 1048 | struct nlattr **attrs) |
| 1049 | { |
| 1050 | struct nlattr *media[TIPC_NLA_MEDIA_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1051 | int err; |
Richard Alpe | 5bfc335 | 2015-02-09 09:50:12 +0100 | [diff] [blame] | 1052 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1053 | if (!attrs[TIPC_NLA_MEDIA]) |
| 1054 | return -EINVAL; |
| 1055 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1056 | err = nla_parse_nested(media, TIPC_NLA_MEDIA_MAX, |
| 1057 | attrs[TIPC_NLA_MEDIA], NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1058 | if (err) |
| 1059 | return err; |
Richard Alpe | 5bfc335 | 2015-02-09 09:50:12 +0100 | [diff] [blame] | 1060 | |
| 1061 | return tipc_add_tlv(msg->rep, TIPC_TLV_MEDIA_NAME, |
| 1062 | nla_data(media[TIPC_NLA_MEDIA_NAME]), |
| 1063 | nla_len(media[TIPC_NLA_MEDIA_NAME])); |
| 1064 | } |
| 1065 | |
Richard Alpe | 4b28cb5 | 2015-02-09 09:50:13 +0100 | [diff] [blame] | 1066 | static int tipc_nl_compat_node_dump(struct tipc_nl_compat_msg *msg, |
| 1067 | struct nlattr **attrs) |
| 1068 | { |
| 1069 | struct tipc_node_info node_info; |
| 1070 | struct nlattr *node[TIPC_NLA_NODE_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1071 | int err; |
Richard Alpe | 4b28cb5 | 2015-02-09 09:50:13 +0100 | [diff] [blame] | 1072 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1073 | if (!attrs[TIPC_NLA_NODE]) |
| 1074 | return -EINVAL; |
| 1075 | |
| 1076 | err = nla_parse_nested(node, TIPC_NLA_NODE_MAX, attrs[TIPC_NLA_NODE], |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1077 | NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1078 | if (err) |
| 1079 | return err; |
Richard Alpe | 4b28cb5 | 2015-02-09 09:50:13 +0100 | [diff] [blame] | 1080 | |
| 1081 | node_info.addr = htonl(nla_get_u32(node[TIPC_NLA_NODE_ADDR])); |
| 1082 | node_info.up = htonl(nla_get_flag(node[TIPC_NLA_NODE_UP])); |
| 1083 | |
| 1084 | return tipc_add_tlv(msg->rep, TIPC_TLV_NODE_INFO, &node_info, |
| 1085 | sizeof(node_info)); |
| 1086 | } |
| 1087 | |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 1088 | static int tipc_nl_compat_net_set(struct tipc_nl_compat_cmd_doit *cmd, |
| 1089 | struct sk_buff *skb, |
Richard Alpe | d7cc75d | 2015-02-09 09:50:14 +0100 | [diff] [blame] | 1090 | struct tipc_nl_compat_msg *msg) |
| 1091 | { |
| 1092 | u32 val; |
| 1093 | struct nlattr *net; |
| 1094 | |
| 1095 | val = ntohl(*(__be32 *)TLV_DATA(msg->req)); |
| 1096 | |
| 1097 | net = nla_nest_start(skb, TIPC_NLA_NET); |
| 1098 | if (!net) |
| 1099 | return -EMSGSIZE; |
| 1100 | |
Richard Alpe | 964f950 | 2015-02-09 09:50:15 +0100 | [diff] [blame] | 1101 | if (msg->cmd == TIPC_CMD_SET_NODE_ADDR) { |
| 1102 | if (nla_put_u32(skb, TIPC_NLA_NET_ADDR, val)) |
| 1103 | return -EMSGSIZE; |
| 1104 | } else if (msg->cmd == TIPC_CMD_SET_NETID) { |
| 1105 | if (nla_put_u32(skb, TIPC_NLA_NET_ID, val)) |
| 1106 | return -EMSGSIZE; |
| 1107 | } |
Richard Alpe | d7cc75d | 2015-02-09 09:50:14 +0100 | [diff] [blame] | 1108 | nla_nest_end(skb, net); |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
Richard Alpe | 3c26181 | 2015-02-09 09:50:16 +0100 | [diff] [blame] | 1113 | static int tipc_nl_compat_net_dump(struct tipc_nl_compat_msg *msg, |
| 1114 | struct nlattr **attrs) |
| 1115 | { |
| 1116 | __be32 id; |
| 1117 | struct nlattr *net[TIPC_NLA_NET_MAX + 1]; |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1118 | int err; |
Richard Alpe | 3c26181 | 2015-02-09 09:50:16 +0100 | [diff] [blame] | 1119 | |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1120 | if (!attrs[TIPC_NLA_NET]) |
| 1121 | return -EINVAL; |
| 1122 | |
| 1123 | err = nla_parse_nested(net, TIPC_NLA_NET_MAX, attrs[TIPC_NLA_NET], |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1124 | NULL, NULL); |
Baozeng Ding | 297f7d2 | 2016-05-24 22:33:24 +0800 | [diff] [blame] | 1125 | if (err) |
| 1126 | return err; |
| 1127 | |
Richard Alpe | 3c26181 | 2015-02-09 09:50:16 +0100 | [diff] [blame] | 1128 | id = htonl(nla_get_u32(net[TIPC_NLA_NET_ID])); |
| 1129 | |
| 1130 | return tipc_add_tlv(msg->rep, TIPC_TLV_UNSIGNED, &id, sizeof(id)); |
| 1131 | } |
| 1132 | |
Richard Alpe | 5a81a63 | 2015-02-09 09:50:17 +0100 | [diff] [blame] | 1133 | static int tipc_cmd_show_stats_compat(struct tipc_nl_compat_msg *msg) |
| 1134 | { |
| 1135 | msg->rep = tipc_tlv_alloc(ULTRA_STRING_MAX_LEN); |
| 1136 | if (!msg->rep) |
| 1137 | return -ENOMEM; |
| 1138 | |
| 1139 | tipc_tlv_init(msg->rep, TIPC_TLV_ULTRA_STRING); |
| 1140 | tipc_tlv_sprintf(msg->rep, "TIPC version " TIPC_MOD_VER "\n"); |
| 1141 | |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1145 | static int tipc_nl_compat_handle(struct tipc_nl_compat_msg *msg) |
| 1146 | { |
| 1147 | struct tipc_nl_compat_cmd_dump dump; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 1148 | struct tipc_nl_compat_cmd_doit doit; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1149 | |
| 1150 | memset(&dump, 0, sizeof(dump)); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 1151 | memset(&doit, 0, sizeof(doit)); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1152 | |
| 1153 | switch (msg->cmd) { |
Richard Alpe | 22ae7cf | 2015-02-09 09:50:18 +0100 | [diff] [blame] | 1154 | case TIPC_CMD_NOOP: |
| 1155 | msg->rep = tipc_tlv_alloc(0); |
| 1156 | if (!msg->rep) |
| 1157 | return -ENOMEM; |
| 1158 | return 0; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1159 | case TIPC_CMD_GET_BEARER_NAMES: |
| 1160 | msg->rep_size = MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME); |
| 1161 | dump.dumpit = tipc_nl_bearer_dump; |
| 1162 | dump.format = tipc_nl_compat_bearer_dump; |
| 1163 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 1164 | case TIPC_CMD_ENABLE_BEARER: |
| 1165 | msg->req_type = TIPC_TLV_BEARER_CONFIG; |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 1166 | doit.doit = __tipc_nl_bearer_enable; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 1167 | doit.transcode = tipc_nl_compat_bearer_enable; |
| 1168 | return tipc_nl_compat_doit(&doit, msg); |
| 1169 | case TIPC_CMD_DISABLE_BEARER: |
| 1170 | msg->req_type = TIPC_TLV_BEARER_NAME; |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 1171 | doit.doit = __tipc_nl_bearer_disable; |
Richard Alpe | 9ab1546 | 2015-02-09 09:50:05 +0100 | [diff] [blame] | 1172 | doit.transcode = tipc_nl_compat_bearer_disable; |
| 1173 | return tipc_nl_compat_doit(&doit, msg); |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 1174 | case TIPC_CMD_SHOW_LINK_STATS: |
| 1175 | msg->req_type = TIPC_TLV_LINK_NAME; |
| 1176 | msg->rep_size = ULTRA_STRING_MAX_LEN; |
| 1177 | msg->rep_type = TIPC_TLV_ULTRA_STRING; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1178 | dump.dumpit = tipc_nl_node_dump_link; |
Richard Alpe | f2b3b2d | 2015-02-09 09:50:06 +0100 | [diff] [blame] | 1179 | dump.format = tipc_nl_compat_link_stat_dump; |
| 1180 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 1181 | case TIPC_CMD_GET_LINKS: |
| 1182 | msg->req_type = TIPC_TLV_NET_ADDR; |
| 1183 | msg->rep_size = ULTRA_STRING_MAX_LEN; |
Jon Paul Maloy | 38206d5 | 2015-11-19 14:30:46 -0500 | [diff] [blame] | 1184 | dump.dumpit = tipc_nl_node_dump_link; |
Richard Alpe | 357ebdb | 2015-02-09 09:50:07 +0100 | [diff] [blame] | 1185 | dump.format = tipc_nl_compat_link_dump; |
| 1186 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 1187 | case TIPC_CMD_SET_LINK_TOL: |
| 1188 | case TIPC_CMD_SET_LINK_PRI: |
| 1189 | case TIPC_CMD_SET_LINK_WINDOW: |
| 1190 | msg->req_type = TIPC_TLV_LINK_CONFIG; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1191 | doit.doit = tipc_nl_node_set_link; |
Richard Alpe | 37e2d48 | 2015-02-09 09:50:08 +0100 | [diff] [blame] | 1192 | doit.transcode = tipc_nl_compat_link_set; |
| 1193 | return tipc_nl_compat_doit(&doit, msg); |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 1194 | case TIPC_CMD_RESET_LINK_STATS: |
| 1195 | msg->req_type = TIPC_TLV_LINK_NAME; |
Jon Paul Maloy | 5be9c08 | 2015-11-19 14:30:45 -0500 | [diff] [blame] | 1196 | doit.doit = tipc_nl_node_reset_link_stats; |
Richard Alpe | 1817877 | 2015-02-09 09:50:09 +0100 | [diff] [blame] | 1197 | doit.transcode = tipc_nl_compat_link_reset_stats; |
| 1198 | return tipc_nl_compat_doit(&doit, msg); |
Richard Alpe | 44a8ae9 | 2015-02-09 09:50:10 +0100 | [diff] [blame] | 1199 | case TIPC_CMD_SHOW_NAME_TABLE: |
| 1200 | msg->req_type = TIPC_TLV_NAME_TBL_QUERY; |
| 1201 | msg->rep_size = ULTRA_STRING_MAX_LEN; |
| 1202 | msg->rep_type = TIPC_TLV_ULTRA_STRING; |
| 1203 | dump.header = tipc_nl_compat_name_table_dump_header; |
| 1204 | dump.dumpit = tipc_nl_name_table_dump; |
| 1205 | dump.format = tipc_nl_compat_name_table_dump; |
| 1206 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 487d2a3 | 2015-02-09 09:50:11 +0100 | [diff] [blame] | 1207 | case TIPC_CMD_SHOW_PORTS: |
| 1208 | msg->rep_size = ULTRA_STRING_MAX_LEN; |
| 1209 | msg->rep_type = TIPC_TLV_ULTRA_STRING; |
| 1210 | dump.dumpit = tipc_nl_sk_dump; |
| 1211 | dump.format = tipc_nl_compat_sk_dump; |
| 1212 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 5bfc335 | 2015-02-09 09:50:12 +0100 | [diff] [blame] | 1213 | case TIPC_CMD_GET_MEDIA_NAMES: |
| 1214 | msg->rep_size = MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME); |
| 1215 | dump.dumpit = tipc_nl_media_dump; |
| 1216 | dump.format = tipc_nl_compat_media_dump; |
| 1217 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 4b28cb5 | 2015-02-09 09:50:13 +0100 | [diff] [blame] | 1218 | case TIPC_CMD_GET_NODES: |
| 1219 | msg->rep_size = ULTRA_STRING_MAX_LEN; |
| 1220 | dump.dumpit = tipc_nl_node_dump; |
| 1221 | dump.format = tipc_nl_compat_node_dump; |
| 1222 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | d7cc75d | 2015-02-09 09:50:14 +0100 | [diff] [blame] | 1223 | case TIPC_CMD_SET_NODE_ADDR: |
| 1224 | msg->req_type = TIPC_TLV_NET_ADDR; |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 1225 | doit.doit = __tipc_nl_net_set; |
Richard Alpe | d7cc75d | 2015-02-09 09:50:14 +0100 | [diff] [blame] | 1226 | doit.transcode = tipc_nl_compat_net_set; |
| 1227 | return tipc_nl_compat_doit(&doit, msg); |
Richard Alpe | 964f950 | 2015-02-09 09:50:15 +0100 | [diff] [blame] | 1228 | case TIPC_CMD_SET_NETID: |
| 1229 | msg->req_type = TIPC_TLV_UNSIGNED; |
Ying Xue | ed4ffdf | 2018-02-14 13:38:04 +0800 | [diff] [blame] | 1230 | doit.doit = __tipc_nl_net_set; |
Richard Alpe | 964f950 | 2015-02-09 09:50:15 +0100 | [diff] [blame] | 1231 | doit.transcode = tipc_nl_compat_net_set; |
| 1232 | return tipc_nl_compat_doit(&doit, msg); |
Richard Alpe | 3c26181 | 2015-02-09 09:50:16 +0100 | [diff] [blame] | 1233 | case TIPC_CMD_GET_NETID: |
| 1234 | msg->rep_size = sizeof(u32); |
| 1235 | dump.dumpit = tipc_nl_net_dump; |
| 1236 | dump.format = tipc_nl_compat_net_dump; |
| 1237 | return tipc_nl_compat_dumpit(&dump, msg); |
Richard Alpe | 5a81a63 | 2015-02-09 09:50:17 +0100 | [diff] [blame] | 1238 | case TIPC_CMD_SHOW_STATS: |
| 1239 | return tipc_cmd_show_stats_compat(msg); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | return -EOPNOTSUPP; |
| 1243 | } |
| 1244 | |
| 1245 | static int tipc_nl_compat_recv(struct sk_buff *skb, struct genl_info *info) |
| 1246 | { |
| 1247 | int err; |
| 1248 | int len; |
| 1249 | struct tipc_nl_compat_msg msg; |
| 1250 | struct nlmsghdr *req_nlh; |
| 1251 | struct nlmsghdr *rep_nlh; |
| 1252 | struct tipc_genlmsghdr *req_userhdr = info->userhdr; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1253 | |
| 1254 | memset(&msg, 0, sizeof(msg)); |
| 1255 | |
| 1256 | req_nlh = (struct nlmsghdr *)skb->data; |
| 1257 | msg.req = nlmsg_data(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN; |
| 1258 | msg.cmd = req_userhdr->cmd; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 1259 | msg.net = genl_info_net(info); |
Florian Westphal | 619b174 | 2016-02-24 17:20:17 +0100 | [diff] [blame] | 1260 | msg.dst_sk = skb->sk; |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1261 | |
| 1262 | if ((msg.cmd & 0xC000) && (!netlink_net_capable(skb, CAP_NET_ADMIN))) { |
| 1263 | msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_NET_ADMIN); |
| 1264 | err = -EACCES; |
| 1265 | goto send; |
| 1266 | } |
| 1267 | |
| 1268 | len = nlmsg_attrlen(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN); |
Ying Xue | 2753ca5 | 2019-01-14 17:22:29 +0800 | [diff] [blame] | 1269 | if (!len || !TLV_OK(msg.req, len)) { |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1270 | msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED); |
| 1271 | err = -EOPNOTSUPP; |
| 1272 | goto send; |
| 1273 | } |
| 1274 | |
| 1275 | err = tipc_nl_compat_handle(&msg); |
Richard Alpe | b063bc5 | 2015-05-06 13:58:56 +0200 | [diff] [blame] | 1276 | if ((err == -EOPNOTSUPP) || (err == -EPERM)) |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1277 | msg.rep = tipc_get_err_tlv(TIPC_CFG_NOT_SUPPORTED); |
| 1278 | else if (err == -EINVAL) |
| 1279 | msg.rep = tipc_get_err_tlv(TIPC_CFG_TLV_ERROR); |
| 1280 | send: |
| 1281 | if (!msg.rep) |
| 1282 | return err; |
| 1283 | |
| 1284 | len = nlmsg_total_size(GENL_HDRLEN + TIPC_GENL_HDRLEN); |
| 1285 | skb_push(msg.rep, len); |
| 1286 | rep_nlh = nlmsg_hdr(msg.rep); |
| 1287 | memcpy(rep_nlh, info->nlhdr, len); |
| 1288 | rep_nlh->nlmsg_len = msg.rep->len; |
Richard Alpe | c3d6fb8 | 2015-05-06 13:58:54 +0200 | [diff] [blame] | 1289 | genlmsg_unicast(msg.net, msg.rep, NETLINK_CB(skb).portid); |
Richard Alpe | d0796d1 | 2015-02-09 09:50:04 +0100 | [diff] [blame] | 1290 | |
| 1291 | return err; |
| 1292 | } |
| 1293 | |
Arvind Yadav | 042a901 | 2017-08-23 16:22:20 +0530 | [diff] [blame] | 1294 | static const struct genl_ops tipc_genl_compat_ops[] = { |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 1295 | { |
| 1296 | .cmd = TIPC_GENL_CMD, |
Richard Alpe | 22ae7cf | 2015-02-09 09:50:18 +0100 | [diff] [blame] | 1297 | .doit = tipc_nl_compat_recv, |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 1298 | }, |
| 1299 | }; |
| 1300 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1301 | static struct genl_family tipc_genl_compat_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1302 | .name = TIPC_GENL_NAME, |
| 1303 | .version = TIPC_GENL_VERSION, |
| 1304 | .hdrsize = TIPC_GENL_HDRLEN, |
| 1305 | .maxattr = 0, |
| 1306 | .netnsok = true, |
| 1307 | .module = THIS_MODULE, |
| 1308 | .ops = tipc_genl_compat_ops, |
| 1309 | .n_ops = ARRAY_SIZE(tipc_genl_compat_ops), |
| 1310 | }; |
| 1311 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1312 | int __init tipc_netlink_compat_start(void) |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 1313 | { |
| 1314 | int res; |
| 1315 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1316 | res = genl_register_family(&tipc_genl_compat_family); |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 1317 | if (res) { |
| 1318 | pr_err("Failed to register legacy compat interface\n"); |
| 1319 | return res; |
| 1320 | } |
| 1321 | |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
| 1325 | void tipc_netlink_compat_stop(void) |
| 1326 | { |
| 1327 | genl_unregister_family(&tipc_genl_compat_family); |
| 1328 | } |